Nearby lessons
20 of 108Python - Type Casting
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
main.py
123
Example 2 - Convert complex to int
main.py
TypeError: can't convert complex to int
Example 3 - Convert True to int
main.py
1
Example 4 - Convert False to int
main.py
0
Example 5 - Convert String to int
main.py
10
Example 6 - Invalid String Conversion
main.py
ValueError: invalid literal for int() with base 10: '10.5'
Example 7 - Invalid Alphabet String
main.py
ValueError: invalid literal for int() with base 10: 'ten'
Example 8 - Invalid Binary String
main.py
ValueError: invalid literal for int() with base 10: '0B1111'
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
main.py
10.0
Example 2 - Convert complex to float
main.py
TypeError: can't convert complex to float
Example 3 - Convert True to float
main.py
1.0
Example 4 - Convert False to float
main.py
0.0
Example 5 - Convert Integer String
main.py
10.0
Example 6 - Convert Decimal String
main.py
10.5
Example 7 - Invalid Alphabet String
main.py
ValueError: could not convert string to float: 'ten'
Example 8 - Invalid Binary String
main.py
ValueError: could not convert string to float: '0B1111'
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
main.py
(10+0j)
Example 2 - Convert float to complex
main.py
(10.5+0j)
Example 3 - Convert True to complex
main.py
(1+0j)
Example 4 - Convert False to complex
main.py
0j
Example 5 - Convert Integer String
main.py
(10+0j)
Example 6 - Convert Decimal String
main.py
(10.5+0j)
Example 7 - Two Integer Arguments
main.py
(10+20j)
Example 8 - Two Float Arguments
main.py
(10.5+20.6j)
Example 9 - Boolean Arguments
main.py
(1+0j)
Example 10 - Boolean Arguments
main.py
1j
Example 11 - Invalid String Arguments
main.py
TypeError
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
main.py
False
Example 2 - Convert Non-zero Integer
main.py
True
Example 3 - Convert Float
main.py
True
Example 4 - Convert 0.0
main.py
False
Example 5 - Convert Complex Number
main.py
True
Example 6 - Convert Zero Complex Number
main.py
False
Example 7 - Convert String "True"
main.py
True
Example 8 - Convert String "False"
main.py
True
Example 9 - Convert Empty String
main.py
False
Example 10 - Convert Non-empty String
main.py
True
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
main.py
'10'
Example 2 - Convert float to String
main.py
'10.5'
Example 3 - Convert complex to String
main.py
'(10+20j)'
Example 4 - Convert True to String
main.py
'True'
Example 5 - Convert False to String
main.py
'False'
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 |