Nearby lessons

20 of 108

Python - 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:

  1. int()
  2. float()
  3. complex()
  4. bool()
  5. str()

1. int() Function

The int() function converts other data types into an integer.

Example 1 - Convert float to int

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
123

Example 2 - Convert complex to int

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
TypeError: can't convert complex to int

Example 3 - Convert True to int

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
1

Example 4 - Convert False to int

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
0

Example 5 - Convert String to int

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
10

Example 6 - Invalid String Conversion

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
ValueError: invalid literal for int() with base 10: '10.5'

Example 7 - Invalid Alphabet String

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
ValueError: invalid literal for int() with base 10: 'ten'

Example 8 - Invalid Binary String

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
ValueError: invalid literal for int() with base 10: '0B1111'

Important Notes about int()

  • Almost every data type can be converted to int, except complex.
  • 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
Python 3 Runtime
Loading Editor...
Output Preview
10.0

Example 2 - Convert complex to float

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
TypeError: can't convert complex to float

Example 3 - Convert True to float

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
1.0

Example 4 - Convert False to float

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
0.0

Example 5 - Convert Integer String

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
10.0

Example 6 - Convert Decimal String

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
10.5

Example 7 - Invalid Alphabet String

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
ValueError: could not convert string to float: 'ten'

Example 8 - Invalid Binary String

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
ValueError: could not convert string to float: '0B1111'

Important Notes about float()

  • Almost every data type can be converted to float, except complex.
  • 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:

  • x represents the real part.
  • y represents the imaginary part.

Example 1 - Convert int to complex

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
(10+0j)

Example 2 - Convert float to complex

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
(10.5+0j)

Example 3 - Convert True to complex

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
(1+0j)

Example 4 - Convert False to complex

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
0j

Example 5 - Convert Integer String

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
(10+0j)

Example 6 - Convert Decimal String

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
(10.5+0j)

Example 7 - Two Integer Arguments

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
(10+20j)

Example 8 - Two Float Arguments

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
(10.5+20.6j)

Example 9 - Boolean Arguments

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
(1+0j)

Example 10 - Boolean Arguments

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
1j

Example 11 - Invalid String Arguments

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
TypeError

Important Notes about complex()

  • The complex() function can be used with one or two arguments.
  • When using complex(x, y), both x and y must 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 is False.
  • If the value is non-zero, the result is True.

Example 1 - Convert Integer 0

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
False

Example 2 - Convert Non-zero Integer

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
True

Example 3 - Convert Float

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
True

Example 4 - Convert 0.0

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
False

Example 5 - Convert Complex Number

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
True

Example 6 - Convert Zero Complex Number

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
False

Example 7 - Convert String "True"

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
True

Example 8 - Convert String "False"

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
True

Example 9 - Convert Empty String

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
False

Example 10 - Convert Non-empty String

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
True

Important Notes about bool()

For string values:

  • Empty string ("") returns False.
  • 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
Python 3 Runtime
Loading Editor...
Output Preview
'10'

Example 2 - Convert float to String

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
'10.5'

Example 3 - Convert complex to String

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
'(10+20j)'

Example 4 - Convert True to String

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
'True'

Example 5 - Convert False to String

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
'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() and float() cannot convert complex numbers.
  • complex() can convert integers, floats, Booleans, and valid numeric strings.
  • bool() returns False for 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

🧠 Test Your Knowledge

10 Questions

Progress: 0 / 10
Keep Going!Python - Immutability