Lesson 12 of 70 – Python Type Casting
17%

Python Type Casting

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.

Note: Type casting is useful when you need to perform an operation using a specific data type.
What is Type Casting?

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))
Output:
20
<class 'int'>
Why Do We Need Type Casting?

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))
int() Function

The int() function converts a compatible value into an integer.

x = "25"

y = int(x)

print(y)
print(type(y))
Output:
25
<class 'int'>
Converting Float to Integer

A float can be converted into an integer using int(). The decimal part is removed.

price = 99.75

number = int(price)

print(number)
Output:
99
Important: int() does not round the number. It removes the fractional part.
String to Integer

A numeric string can be converted into an integer.

number = "100"

number = int(number)

print(number + 50)
Output:
150

This is useful when numeric data is received as text.

float() Function

The float() function converts a compatible value into a floating-point number.

x = "25.50"

y = float(x)

print(y)
print(type(y))
Output:
25.5
<class 'float'>
Integer to Float

An integer can be converted into a float using float().

number = 25

result = float(number)

print(result)
print(type(result))
Output:
25.0
<class 'float'>
String to Float

A numeric string containing a decimal value can be converted into a float.

price = "99.50"

price = float(price)

print(price)
Output:
99.5
str() Function

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'>
Integer to String

An integer can be converted into a string using str().

age = 25

message = "My age is " + str(age)

print(message)
Output:
My age is 25

Without str(), Python cannot directly concatenate an integer with a string using the + operator.

bool() Function

The bool() function converts a value into a Boolean value: True or False.

x = 10

print(bool(x))
Output:
True

Generally, zero and empty values are considered false, while non-zero and non-empty values are considered true.

Boolean Conversion Examples
print(bool(1))
print(bool(0))
print(bool(10))
print(bool(-5))
Output:
True
False
True
True
String to Boolean

Strings can also be converted to Boolean values.

print(bool("Hello"))
print(bool(""))
Output:
True
False

An empty string is considered False. A non-empty string is considered True.

Type Casting with User Input

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
Output:
Your age is: 20
Calculating with User Input
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:

Output:
Sum: 30
Multiple Type Conversions

A value can be converted through multiple data types.

x = 10

x = float(x)

print(x)

x = str(x)

print(x)
print(type(x))
Output:
10.0
10.0
<class 'str'>
Invalid Type Conversion

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.

Important: The value must be compatible with the target data type.
Type Casting Example
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:

  • name remains a string.
  • age is converted into an integer.
  • height is converted into a float.
Common Type Casting Functions
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)
Key Points
  • Type casting means converting one data type into another.
  • int() converts compatible values to integers.
  • float() converts compatible values to floating-point numbers.
  • str() converts values to strings.
  • bool() converts values to Boolean values.
  • input() normally returns a string.
  • Type casting is commonly used when working with user input.
  • Invalid conversions can cause errors such as ValueError.

🧠 Quick Quiz

Question: Which function converts a string into an integer?