◐ Shell
clean mode source ↗

Python Casting: Type Conversion and Type Casting

In Python, we can convert one type of variable to another type. This conversion is called type casting or type conversion.

In casting, we convert variables declared in specific data types to the different data types.

Python performs the following two types of casting.

  • Implicit casting: The Python interpreter automatically performs an implicit Type conversion, which avoids loss of data.
  • Explicit casting: The explicit type conversion is performed by the user using built-in functions.

To perform a type casting, we are going to use the following built-in functions

  1. int(): convert any type variable to the integer type.
  2. float(): convert any type variable to the float type.
  3. complex(): convert any type variable to the complex type.
  4. bool(): convert any type variable to the bool type.
  5. str(): convert any type variable to the string type.

In type casting, data loss may occur because we enforce the object to a specific data type.

In int type conversion, we use the int() function to convert variables of other types to int type. Variable can be of any type such as float, string, bool.

While performing int type conversion, we need to remember the following points.

  1. When converting string type to int type, a string must contain integral value only and should be base-10.
  2. We can convert any type to int type, but we cannot perform complex to int type.

Casting float value to an integer

Casting Boolean value to an integer

Casting a string to an integer

When converting string type to int type, a string must contain integral value only and should be base-10. If you try to convert 

Example

Float type conversion

In float type conversion we use a built-in function  float() . This function converts variables of other types to float types.

Casting integer to float

Casting Boolean to float

Casting string to float

While performing float type conversion, we need to remember some points.

  1. We can convert any type to float type, but we cannot cast complex to float type.
  2. While converting string type to float type, a string must contain an integer/decimal value of base-10.

Complex type conversion

In complex type conversion, we use the built-in function complex() to convert values from other types to the complex type. Value can be any type including of  int, float, bool, str.

The complex function has the following two forms for conversion.

  • complex(x): To convert a value x into a complex type. In this form, the real value is  x, and the imaginary value is 0.
  • complex(x, y): To convert the value x and y into a complex type. In this form, the real value is x, and the imaginary is y.

Casting integer type to complex type

Casting float type to complex type

Casting Boolean type to complex type

Bool type conversion

We use the built-in function bool() to convert values of other types to bool types. This function returns two values, either True or False.

  • We can convert any type of values to bool type, and the output for all values will be True, Except 0, which is False.
  • If you convert an empty string to a boolean it will be converted to boolean False.

The bool True is 1 and False is 0. Every non-zero value is treated as True.

Casting integer to Boolean type

Casting float to Boolean type

Casting string to Boolean type

Casting complex type to Boolean type

String type conversion

In str type conversion, we use the built-in function str() to convert converts variables of other types to a string type. This function returns the string type of object (value).

Casting int to str type

Casting float type to str type

Casting complex type to str  type

Casting bool type to str type

About Vishal

I’m Vishal Hule, the Founder of PYnative.com. As a Python developer, I enjoy assisting students, developers, and learners. Follow me on Twitter.

Python Exercises and Quizzes

Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.

  • 15+ Topic-specific Exercises and Quizzes
  • Each Exercise contains 25+ questions
  • Each Quiz contains 25 MCQ