Nearby lessons
27 of 159Python - Type Casting
- Define type casting and why it is used
- Convert values to integers with int()
- Convert values to floating-point numbers with float()
- Create complex numbers with complex()
- Convert values to Boolean and string with bool() and str()
- Identify conversions that raise errors
Introduction
Sometimes we need to convert a value from one data type to another.
This process is called Type Casting or Type Conversion.
Python provides built-in functions to perform type conversion.
Python supports the following type casting functions:
int()float()complex()bool()str()
1. int() Function
The int() function converts other data types into an integer.
Example 1 - Convert float to int
Example 2 - Convert complex to int
Example 3 - Convert True to int
Example 4 - Convert False to int
Example 5 - Convert String to int
Example 6 - Invalid String Conversion
Example 7 - Invalid Alphabet String
Example 8 - Invalid Binary String
Important Notes about int()
- Almost every data type can be converted to
int, exceptcomplex. - When converting a string, it must contain only a valid integer value.
- The string must represent a base-10 integer.
2. float() Function
The float() function converts values into the float data type.
Example 1 - Convert int to float
Example 2 - Convert complex to float
Example 3 - Convert True to float
Example 4 - Convert False to float
Example 5 - Convert Integer String
Example 6 - Convert Decimal String
Example 7 - Invalid Alphabet String
Example 8 - Invalid Binary String
Important Notes about float()
- Almost every data type can be converted to
float, exceptcomplex. - When converting a string, it must contain either a valid integer or a valid floating-point value.
- The string must represent a base-10 number.
3. complex() Function
The complex() function converts values into the complex data type.
Syntax:
complex(x)
or
complex(x, y)
Where:
xrepresents the real part.yrepresents the imaginary part.
Example 1 - Convert int to complex
Example 2 - Convert float to complex
Example 3 - Convert True to complex
Example 4 - Convert False to complex
Example 5 - Convert Integer String
Example 6 - Convert Decimal String
Example 7 - Two Integer Arguments
Example 8 - Two Float Arguments
Example 9 - Boolean Arguments
Example 10 - Boolean Arguments
Example 11 - Invalid String Arguments
Important Notes about complex()
- The
complex()function can be used with one or two arguments. - When using
complex(x, y), bothxandymust be numeric values or Boolean values. - String values are not allowed as the second argument.
- If a string is used as the first argument, it must represent a valid integer or floating-point number.
4. bool() Function
The bool() function converts values into the bool data type.
Rules:
- If the value is
0, the result isFalse. - If the value is non-zero, the result is
True.
Example 1 - Convert Integer 0
Example 2 - Convert Non-zero Integer
Example 3 - Convert Float
Example 4 - Convert 0.0
Example 5 - Convert Complex Number
Example 6 - Convert Zero Complex Number
Example 7 - Convert String "True"
Example 8 - Convert String "False"
Example 9 - Convert Empty String
Example 10 - Convert Non-empty String
Important Notes about bool()
For string values:
- Empty string (
"") returnsFalse. - Any non-empty string returns
True.
For example, "False" returns True because it is a non-empty string.
5. str() Function
The str() function converts values into the str (string) data type.
Example 1 - Convert int to String
Example 2 - Convert float to String
Example 3 - Convert complex to String
Example 4 - Convert True to String
Example 5 - Convert False to String
Important Notes about str()
- The
str()function can convert any Python value into a string. - The returned value is always of type
str.
Type Conversion Summary
| Function | Purpose |
|---|---|
int() |
Converts a value to an integer. |
float() |
Converts a value to a floating-point number. |
complex() |
Converts a value to a complex number. |
bool() |
Converts a value to a Boolean value. |
str() |
Converts a value to a string. |
Complete Type Conversion Table
| Source Type | int() | float() | complex() | bool() | str() |
|---|---|---|---|---|---|
| int | ✔ | ✔ | ✔ | ✔ | ✔ |
| float | ✔ | ✔ | ✔ | ✔ | ✔ |
| complex | ✘ | ✘ | ✔ | ✔ | ✔ |
| bool | ✔ | ✔ | ✔ | ✔ | ✔ |
| str | ✔* | ✔* | ✔* | ✔ | ✔ |
Note: String conversion to int(), float(), and complex() works only if the string contains a valid numeric value.
Important Points
- Type casting converts a value from one data type to another.
- Python provides five built-in type casting functions.
int()andfloat()cannot convert complex numbers.complex()can convert integers, floats, Booleans, and valid numeric strings.bool()returnsFalsefor zero values and empty strings.- Any non-zero number or non-empty string returns
True. str()can convert almost every Python object into a string.
Key Points
- Python supports automatic as well as explicit type conversion.
- Use
int()to convert values into integers. - Use
float()to convert values into floating-point numbers. - Use
complex()to create complex numbers. - Use
bool()to convert values into Boolean values. - Use
str()to convert values into strings. - Always make sure the value is valid before converting it to another data type.
Quick Summary
| Function | Converts To |
|---|---|
int() |
Integer |
float() |
Floating-point Number |
complex() |
Complex Number |
bool() |
Boolean Value |
str() |
String |
- Type casting converts a value from one data type to another
- int() converts a value to an integer, but cannot convert a complex number
- float() converts a value to a floating-point number
- complex() creates a complex number from one or two arguments
- bool() returns False for zero values and empty strings
- str() converts almost any Python value into a string