Type casting can be tricky for beginners 🔄. It’s easy to forget that you can’t mix strings with numbers or accidentally end up with the wrong data type 🧩. Many errors happen simply because values weren’t converted properly 🛑.
In this blog post, we will cover how to convert values between different data types in Python, including numbers, strings, booleans, and collections like lists, tuples, and sets. You’ll also learn how to avoid common mistakes and write cleaner, more flexible code.
🚀 Let’s explore how type casting works in Python and learn how to switch between data types with confidence!
Video Tutorial
Casting to Integer
Decimal Number (Float) –> Integer
num = int(3.9)
print(num) # Output: 3
💡 Key Points:
int(3.9)removes the decimal part and returns3.- This is called truncation — it does not round, it simply chops off the decimal.
String –> Integer
# num_str = "10"
# print(num_str + 5) # Error! Why? Because "10" is text, and 5 is a number.
num = int("10")
print(num + 5) # Output: 15
💡 Key Points:
- A string like
"10"needs to be converted to an integer before adding to a number. int("10")gives the number10, so10 + 5 = 15.- If you try
"10" + 5, Python will throw a TypeError.
Boolean –> Integer
print(int(True)) # Output: 1
print(int(False)) # Output: 0
💡 Key Points:
Truebecomes1andFalsebecomes0when cast to integers.- This is useful in counting or flag-based logic.
Casting to Float
Integer –> Float
num = float(5)
print(num) # Output: 5.0
💡 Key Points:
- Use
float()to convert whole numbers to decimals. - Helps avoid integer division issues when you want exact results.
Casting to String
Number –> String
# Number --> String
num = 25
text = str(num)
print(text)
print(type(text)) # Output: <class 'str'>
💡 Key Points:
str(num)converts the number25into the string"25".type(text)confirms that the result is a string.
Combining strings with numbers
# Error
age = 30
# message = "I am " + age + " years old." # Error! Mixing string and integer causes a TypeError
# Solution
age = 30
message = "I am " + str(age) + " years old."
print(message) # Output: I am 30 years old.
💡 Key Points:
- Use
str()to turn numbers or other data into strings. - Required when joining text with numbers using
+.
Casting to Boolean
Numbers –> Boolean
print(bool(1)) # Output: True
print(bool(100)) # Output: True
print(bool(-1)) # Output: True
print(bool(0)) # Output: False
💡 Key Points:
bool(1),bool(100),bool(-1)→ all returnTrue.- Only
0is consideredFalse. - All non-zero numbers are treated as
True.
Strings –> Boolean
print(bool("Hello")) # Output: True
print(bool("")) # Output: False (empty string)
print(bool(" ")) # Output: True (a space counts as a character)
💡 Key Points:
- A non-empty string →
True. - An empty string →
False. - Even a single space (
" ") counts as a valid character →True.
Casting to List
Tuple –> List
my_tuple = (1, 2, 3)
my_list = list(my_tuple)
print(my_list) # Output: [1, 2, 3]
💡 Key Points:
list()converts a tuple into a list.- Tuples are immutable, but lists are changeable, so this is useful when you want to modify the contents.
my_list.append(4) # Adds 4 to the list
print(my_list) # Output: [1, 2, 3, 4]
Now you can use list methods like .append() to change the data.
String –> List
word = "hello"
letters = list(word)
print(letters) # Output: ['h', 'e', 'l', 'l', 'o']
💡 Key Points:
- Converts a string into a list of its individual characters.
Set –> List
my_set = {10, 20, 30}
my_list = list(my_set)
print(my_list) # Output: [10, 20, 30] (Order may vary)
💡 Key Points:
- Converts a set into a list.
- Since sets are unordered, the resulting list’s order may differ.
Casting to Tuple
List –> Tuple
my_list = [1, 2, 3]
my_tuple = tuple(my_list)
print(my_tuple) # Output: (1, 2, 3)
💡 Key Points:
tuple()converts a list into a tuple.- Tuples are immutable, meaning their contents can’t be changed.
- This is helpful when you want to protect the data from being modified.
# my_tuple.append(4) # Error! Tuples don’t support append()
Tuples do not have methods like .append() since they are not meant to change.
String –> Tuple
word = "Python"
letters = tuple(word)
print(letters) # Output: ('P', 'y', 't', 'h', 'o', 'n')
💡 Key Points:
- Converts a string into a tuple of characters.
Set –> Tuple
my_set = {10, 20, 30}
my_tuple = tuple(my_set)
print(my_tuple) # Output: (10, 20, 30) (Order may vary)
💡 Key Points:
- Order is not guaranteed because sets are unordered.
Casting to Set
List –> Set
my_list = [1, 2, 2, 3, 3, 4]
my_set = set(my_list)
print(my_set) # Output: {1, 2, 3, 4}
💡 Key Points:
set()removes duplicate values from the list.- Sets are unordered collections of unique elements.
Keep Order But Remove Duplicates
my_list = [8, 3, 8, 7, 3, 5]
unique_list = list(dict.fromkeys(my_list))
print(unique_list) # Output: [8, 3, 7, 5]
💡 Key Points:
dict.fromkeys()creates a dictionary where keys are unique and ordered, then it’s turned back into a list.
Code
👉 Download












