Python Type Casting

About Type Casting in Python

In Python, type casting is the process of converting a variable from one data type to another type.

Python has built-in functions to convert data types.

The built-in functions include int(), float(), str(), list(), set(), tuple(), and dict().

Built-in Functions for Type Casting in Python

Converting To Integer Type

int(): int() function is used to convert numeric string and float data type to integer type.

Please note that string must contain numeric number. If the string contains any character, an error will be raised.

Example of int() function
Output of the above example
    30
    20
                                

Converting To Float Type

float(): float() function is used to convert numeric string and integer data type to float type.

Example of float() function
Output of the above example
    30.0
    25.0
                                

Converting To String Type

str(): str() function is used to convert any data type to string type.

Example of str() function
Output of the above example
    42
    3.14
    [1, 2, 3]
                                

Converting To Bool Type

bool(): bool() function is used to convert a variable to a boolean type.

bool() function converts numeric values and non-empty objects to True, while numeric zero, empty objects, and None are converted to False.

Example of bool() function
Output of the above example
    True
    False
    True
    False
    True
    False
                                

Converting To List Type

list(): list() function is used to convert other iterable objects like tuples, sets, strings, or ranges into a list.

The list() function is also used to create a new list object.

Example of list() function
Output of the above example
    [1, 2, 3, 4, 5]
    ['H', 'e', 'l', 'l', 'o']
    [1, 2, 3, 4, 5, 6, 7, 8, 9]

                                

Converting To Tuple Type

tuple(): tuple() function is used to convert other iterable objects like lists, sets, strings, or ranges into a tuple.

The tuple() function is also used to create a new tuple object.

Example of tuple() function
Output of the above example
    (1, 2, 3, 4, 5)
    ('H', 'e', 'l', 'l', 'o')
    (1, 2, 3, 4, 5, 6, 7, 8, 9)

                                

Converting To Set Type

set(): set() function is used to convert other iterable objects like list, tuples, strings, or ranges into a set.

The set() function is also used to create a new set object.

Example of set() function:
Output of the above example
    {1, 2, 3, 4, 5}
    {'l', 'e', 'H', 'o'}
    {1, 2, 3, 4, 5, 6, 7, 8, 9}

                                

Converting To Dictionary Type

dict(): dict() function is used to convert an iterable of key-value pairs to a dictionary type.

The dict() function is also used to create a new dictionary object.

Example of set() function:
Output of the above example
    {'x': 10, 'y': 11}
    {'a': 1, 'b': 2}

                                

Python Compiler For Live Practice

Practice the above Python codes in the following Python Editor. Create more Python variables and cast their types in other types in the following code editor and click Execute button. Output will be displayed in Python Code Output section.

Python logo Python Editor
Python Code Output