Type Casting (Type Conversion)
On this page
Understanding Type ConversionWhy Type Conversion MattersConverting Between Numbers and StringsType Conversion with ConditionsMini Project: BMI CalculatorIn Python, every piece of data has a type. Numbers, text, and lists are all different types of data, and each type behaves in its own way. Sometimes, a program needs to convert a value from one type to another—for example, when reading user input (which always arrives as text) but needing to perform a calculation. This process is called type casting or type conversion.
Understanding Type Conversion
Type conversion means transforming one data type into another. Python provides simple built-in functions for this:
int()converts a value into an integerfloat()converts a value into a floating-point numberstr()converts a value into a string
Here’s a simple example showing how conversions work:
print(int("42"))
print(float("3.14"))
print(str(25))Output
42
3.14
25
In the first two lines, text values are turned into numbers that Python can use for arithmetic. In the third line, a number is turned into text that can be printed or combined with other strings.
Why Type Conversion Matters
When data is entered through the input() function, Python treats it as a string, even if the user types numbers. Trying to use that string in a calculation will result in an error unless it’s converted.
age = input("Enter your age: ")
next_year = age + 1This leads to this error:
TypeError: can only concatenate str (not "int") to str
Here, Python tries to add a string to a number, which is not allowed.
To fix this, convert age to an integer before performing arithmetic:
age = int(input("Enter your age: "))
next_year = age + 1
print("Next year you will be", next_year)Output:
Enter your age: 25
Next year you will be 26
Now Python knows that age is a number, so it can perform the calculation correctly.
Converting Between Numbers and Strings
Conversion works both ways. You can also turn numbers into strings, which is useful when combining them with text in messages or output.
temperature = 25
print("The temperature is " + str(temperature) + "°C today.")Without str(), Python would raise an error because it cannot combine text and numbers directly in the same expression.
Type Conversion with Conditions
Sometimes, after converting input to numbers, you might want your program to react differently based on the result. The if...else statement helps with this by checking a condition and deciding which code to run.
Example Run:
Enter a number: 12
Positive numbernumber = int(input("Enter a number: "))
if number > 0:
print("Positive number")
else:
print("Zero or negative number")Mini Project: BMI Calculator
Write a program that calculates a person’s Body Mass Index (BMI) based on their weight and height.
- Ask the user to enter their weight in kilograms and height in meters using the
input()function. - Remember that
input()returns a string. - Apply the BMI formula: $ = $
- Use
round(bmi, 1)to display one decimal place.round(value, digits)rounds a number to the requested precision.
After Conditional Statements, extend this arithmetic exercise with adult BMI bands: below 18.5, 18.5 to below 25, 25 to below 30, and 30 or above. The sample labels follow the CDC adult BMI categories. These are screening categories, not a health assessment on their own.
Example Runs:
Run 1:
Enter your weight (kg): 50
Enter your height (m): 1.75
Your BMI is: 16.3
BMI category: underweight.
Run 2:
Enter your weight (kg): 68
Enter your height (m): 1.75
Your BMI is: 22.2
BMI category: healthy weight range.
Run 3:
Enter your weight (kg): 90
Enter your height (m): 1.70
Your BMI is: 31.1
BMI category: obesity.