Stewdent.eu
▾

Type Conversion

Type conversion changes a value from one data type to another. Python provides built-in functions to convert between integers, floats, strings, and Booleans.

Last updated: 10 December 2025

★★☆☆☆ Level 2computingprogrammingdata-typestype conversioncasting4 min read

Report an error

Programs constantly deal with different types of data. Sometimes you need to combine a number with text, or you receive text from the user but need to do arithmetic with it. Type conversion is the process of changing a value from one type to another so that your program can work with it correctly.

Why Type Conversion Matters

When you read input with input(), Python always gives you a string. If the user types 42, you receive the string "42", not the number 42. Trying to add it to another number will fail:

x = input("Enter a number: ")   # x is "42" (string)
print(x + 10)                   # TypeError: cannot add str and int

You need to convert "42" to 42 first.

Explicit Conversion Functions

Python provides functions for the most common conversions:

FunctionConverts toExample
int()Integerint("42") → 42
float()Floatfloat("3.14") → 3.14
str()Stringstr(100) → "100"
bool()Booleanbool(0) → False
age = int(input("Enter your age: "))
height = float(input("Enter your height in metres: "))
print(f"In 10 years you will be {age + 10}.")
Note

Converting between types is sometimes called casting, especially in languages like Java and C#. In Python it is more often called type conversion.

Integer and Float Conversion

# float to int (truncates — drops the decimal part)
print(int(3.9))     # 3  (NOT rounded, just truncated)
print(int(-2.7))    # -2

# int to float
print(float(7))     # 7.0
Warning

int() does not round — it simply removes the decimal part. int(3.9) gives 3, not 4. If you need rounding, use round() first.

String to Number Conversion

int() and float() can parse strings that look like numbers:

print(int("100"))       # 100
print(float("3.14"))    # 3.14
print(int("  25  "))    # 25 (strips whitespace)

But if the string contains non-numeric characters, Python raises a ValueError:

int("hello")      # ValueError
int("3.14")       # ValueError — use float() instead
float("12abc")    # ValueError
Tip

When you are not sure whether user input is a valid number, use a try-except block to catch the ValueError and ask again: try: n = int(input()) except ValueError: print("Please enter a number.").

Number to String Conversion

You often need to convert a number to a string to join it with other text:

score = 95
message = "Your score is: " + str(score)
print(message)    # Your score is: 95

F-strings handle this automatically, which is why they are usually preferred:

print(f"Your score is: {score}")

Boolean Conversion

bool() converts any value to True or False:

print(bool(0))       # False
print(bool(1))       # True
print(bool(-5))      # True (any non-zero number)
print(bool(""))      # False
print(bool("hi"))    # True (any non-empty string)
print(bool([]))      # False
print(bool([1,2]))   # True

Implicit Conversion

Python sometimes converts types automatically. For example, mixing int and float in arithmetic:

result = 5 + 2.0    # Python converts 5 to 5.0 automatically
print(result)       # 7.0
print(type(result)) # <class 'float'>

Practice Problems

What is the output of int(9.8) + int('3') and what type is the result?

Show answer
int(9.8) gives 9 (truncated, not rounded) and int('3') gives 3. 9 + 3 = 12. The result is an integer (int).

A student writes total = '5' + '3' expecting to get 8. What do they actually get, and how do they fix it?

Show answer
They get '53' because Python concatenates strings when you use + on two strings. The fix is to convert both to integers first: total = int('5') + int('3'), which gives 8.