Type casting means converting a value from one data type to another. Python provides built-in functions such as int(), float(), str(), and bool() for converting values between different data types.
Type casting is the process of converting one data type into another data type.
For example:
age = "20"
age = int(age)
print(age)
print(type(age))
Sometimes data is available in one type but we need to use it as another type.
For example, input received from the input() function is normally a string.
age = input("Enter your age: ")
print(type(age))
If the user enters 20, Python treats it as a string. We can convert it into an integer using int().
age = int(input("Enter your age: "))
print(type(age))
The int() function converts a compatible value into an integer.
x = "25"
y = int(x)
print(y)
print(type(y))
A float can be converted into an integer using int(). The decimal part is removed.
price = 99.75
number = int(price)
print(number)
A numeric string can be converted into an integer.
number = "100"
number = int(number)
print(number + 50)
This is useful when numeric data is received as text.
The float() function converts a compatible value into a floating-point number.
x = "25.50"
y = float(x)
print(y)
print(type(y))
An integer can be converted into a float using float().
number = 25
result = float(number)
print(result)
print(type(result))
A numeric string containing a decimal value can be converted into a float.
price = "99.50"
price = float(price)
print(price)
The str() function converts a value into a string.
age = 20
age_text = str(age)
print(age_text)
print(type(age_text))
An integer can be converted into a string using str().
age = 25
message = "My age is " + str(age)
print(message)
Without str(), Python cannot directly concatenate an integer with a string using the + operator.
The bool() function converts a value into a Boolean value: True or False.
x = 10
print(bool(x))
Generally, zero and empty values are considered false, while non-zero and non-empty values are considered true.
print(bool(1))
print(bool(0))
print(bool(10))
print(bool(-5))
Strings can also be converted to Boolean values.
print(bool("Hello"))
print(bool(""))
An empty string is considered False. A non-empty string is considered True.
The input() function returns data as a string. We can use type casting when we need numeric input.
age = int(input("Enter your age: "))
print("Your age is:", age)
If the user enters:
20
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
sum = num1 + num2
print("Sum:", sum)
If the user enters 10 and 20:
A value can be converted through multiple data types.
x = 10
x = float(x)
print(x)
x = str(x)
print(x)
print(type(x))
Not every value can be converted into every data type. For example, a normal text string cannot be converted into an integer.
number = int("Hello")
print(number)
This produces a ValueError because "Hello" is not a valid integer representation.
name = input("Enter your name: ")
age = int(input("Enter your age: "))
height = float(input("Enter your height: "))
print("Name:", name)
print("Age:", age)
print("Height:", height)
Here:
| Function | Purpose | Example |
|---|---|---|
| int() | Converts a value to integer | int("10") |
| float() | Converts a value to float | float("10.5") |
| str() | Converts a value to string | str(10) |
| bool() | Converts a value to Boolean | bool(1) |
Question: Which function converts a string into an integer?