While working with Python, you'll often need to change a value from one data type to another.
For example, input() gives us a string, but if the user enters their age, we probably want to work with it as an integer.
This is where type conversion comes in.
What is Type Conversion?
Type conversion is the process of changing a value from one data type to another.
For example:
age = "20"
age = int(age)
print(age)Here, "20" was originally a string, and int() converted it into an integer.
Common Type Conversion Functions
Python provides several built-in functions for converting values:
Function | Converts To | Example |
|---|---|---|
| Integer |
|
| Float |
|
| String |
|
| Boolean |
|
Let's look at them one by one.
Converting to Integer with int()
Use int() when you want to convert a value into an integer.
number = int("25")
print(number)
print(type(number))Output:
25
<class 'int'>You can also convert a float to an integer:
number = int(10.8)
print(number)Output:
10Notice that int() removes the decimal part rather than rounding it.
Converting to Float with float()
The float() function converts a value into a floating-point number.
price = float("99.99")
print(price)Output:
99.99It can also convert an integer:
number = float(10)
print(number)Output:
10.0Converting to String with str()
The str() function converts a value into a string.
age = 20
age_text = str(age)
print(age_text)
print(type(age_text))Output:
20
<class 'str'>This is useful when you need to combine a number with text.
age = 20
print("I am " + str(age) + " years old")Output:
I am 20 years oldConverting to Boolean with bool()
The bool() function converts a value into either True or False.
For example:
print(bool(1))
print(bool(0))Output:
True
FalseIn general, many values are considered truthy, while values such as 0, "", None, and empty collections are falsy.
For example:
print(bool("Hello"))
print(bool(""))Output:
True
FalseType Conversion with input()
This is one of the most common situations where you'll use type conversion.
Remember, input() always returns a string.
age = input("Enter your age: ")Even if the user enters 20, age is still a string.
If we want an integer, we can convert it immediately:
age = int(input("Enter your age: "))
print(age + 1)If the user enters 20, the output is:
21Similarly, for decimal values:
price = float(input("Enter the price: "))
print(price)Converting Between Different Types
You can convert values between several compatible types:
number = 10
print(float(number))
print(str(number))
print(bool(number))Output:
10.0
10
TrueThe original number is still an integer. The conversions produce new values.
Invalid Conversion
Not every value can be converted successfully.
For example:
age = int("hello")This causes a ValueError because "hello" isn't a valid integer.
Similarly:
number = float("Python")will also cause an error.
So, make sure the value you're converting is compatible with the target type.
A Practical Example
Let's create a simple program that calculates the total cost:
price = float(input("Enter price: "))
quantity = int(input("Enter quantity: "))
total = price * quantity
print("Total:", total)If the user enters:
Enter price: 99.50
Enter quantity: 3The result will be:
Total: 298.5Type conversion may look like a small concept, but you'll use it constantly in Python—especially when working with user input, calculations, files, and data from external sources.