Python Built‑in Functions for Data Type Casting
This article explains how to use Python's built‑in casting functions—int(), float(), str(), bool(), list(), tuple(), set() and dict()—to convert between numeric, string, boolean, sequence, and mapping types, providing clear code examples for each conversion scenario.
Python provides several built‑in functions such as int() , float() , str() , bool() , list() , tuple() , set() and dict() to explicitly cast objects between data types.
int() and float()
Convert strings to numbers:
num_str = "42"
num = int(num_str)
print(num) # 42Convert strings to floating‑point numbers:
pi_str = "3.14159"
pi = float(pi_str)
print(pi) # 3.14159Convert booleans and floats to integers (True → 1, False → 0, truncates decimals):
is_true = True
num = int(is_true)
print(num) # 1 pi = 3.14159
num = int(pi)
print(num) # 3Convert integers to floats:
num = 42
pi = float(num)
print(pi) # 42.0str()
Convert various objects to their string representation:
num = 42
num_str = str(num)
print(num_str) # "42" pi = 3.14159
pi_str = str(pi)
print(pi_str) # "3.14159" is_true = True
is_true_str = str(is_true)
print(is_true_str) # "True" my_list = [1, 2, 3]
list_str = str(my_list)
print(list_str) # "[1, 2, 3]" my_dict = {'a': 1, 'b': 2, 'c': 3}
dict_str = str(my_dict)
print(dict_str) # "{'a': 1, 'b': 2, 'c': 3}"bool()
Convert values to boolean based on truthiness:
num = 42
is_true = bool(num)
print(is_true) # True pi = 3.14159
is_true = bool(pi)
print(is_true) # True empty_str = ""
is_true = bool(empty_str)
print(is_true) # False nonempty_str = "Hello"
is_true = bool(nonempty_str)
print(is_true) # True empty_list = []
is_true = bool(empty_list)
print(is_true) # Falselist()
Convert iterables to lists:
text = "Hello, World!"
char_list = list(text)
print(char_list) # ['H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!'] tuple_data = (1, 2, 3, 4, 5)
list_data = list(tuple_data)
print(list_data) # [1, 2, 3, 4, 5] set_data = {1, 2, 3, 4, 5}
list_data = list(set_data)
print(list_data) # order may vary sentence = "I love Python"
word_list = sentence.split()
print(word_list) # ['I', 'love', 'Python'] numbers = "1 2 3 4 5"
number_list = [int(num) for num in numbers.split()]
print(number_list) # [1, 2, 3, 4, 5]tuple()
Convert iterables to tuples:
list_data = [1, 2, 3, 4, 5]
tuple_data = tuple(list_data)
print(tuple_data) # (1, 2, 3, 4, 5) text = "Hello, World!"
char_tuple = tuple(text)
print(char_tuple) # ('H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!') set_data = {1, 2, 3, 4, 5}
tuple_data = tuple(set_data)
print(tuple_data) # order may vary sentence = "I love Python"
word_tuple = tuple(sentence.split())
print(word_tuple) # ('I', 'love', 'Python') numbers = "1 2 3 4 5"
number_tuple = tuple(int(num) for num in numbers.split())
print(number_tuple) # (1, 2, 3, 4, 5)set()
Convert iterables to sets (unique, unordered):
list_data = [1, 2, 3, 4, 5]
set_data = set(list_data)
print(set_data) # {1, 2, 3, 4, 5} tuple_data = (1, 2, 3, 4, 5)
set_data = set(tuple_data)
print(set_data) # {1, 2, 3, 4, 5} text = "Hello, World!"
set_data = set(text)
print(set_data) # unique characters sentence = "I love Python"
word_set = set(sentence.split())
print(word_set) # {'I', 'love', 'Python'} list_data = [1, 2, 3, 3, 4, 4, 5, 5]
unique_set = set(list_data)
print(unique_set) # {1, 2, 3, 4, 5}dict()
Convert iterable key‑value pairs to dictionaries:
list_data = [('a', 1), ('b', 2), ('c', 3)]
dict_data = dict(list_data)
print(dict_data) # {'a': 1, 'b': 2, 'c': 3} tuple_data = (('a', 1), ('b', 2), ('c', 3))
dict_data = dict(tuple_data)
print(dict_data) # {'a': 1, 'b': 2, 'c': 3} keys = ['a', 'b', 'c']
values = [1, 2, 3]
dict_data = dict(zip(keys, values))
print(dict_data) # {'a': 1, 'b': 2, 'c': 3} set_data = {('a', 1), ('b', 2), ('c', 3)}
dict_data = dict(set_data)
print(dict_data) # {'a': 1, 'b': 2, 'c': 3} keys = ['a', 'b', 'c']
values = [1, 2, 3]
dict_data = {key: value for key, value in zip(keys, values)}
print(dict_data) # {'a': 1, 'b': 2, 'c': 3}Test Development Learning Exchange
Test Development Learning Exchange
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.