Python Type Conversions

Let’s look at type conversions

  1. Notice

    A conversion asks Python for a value of another type. The text must have a suitable format; int("three") cannot produce an integer.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
text = '3'
number = int(text)
print(number + 1)
Live state

Top-level scope

text→Object 1
number→Object 2
Object 1 · str'3'Object 2 · int3
This step

A conversion asks Python for a value of another type. The text must have a suitable format; int("three") cannot produce an integer.

Picture type conversions

A conversion asks Python for a value of another type. The text must have a suitable format; int("three") cannot produce an integer.

Image brief

Alt text
A diagram for Python Type Conversions. Instructions: text = '3' number = int(text) print(number + 1). Displayed result: 4. The final example bindings are text --> object 1 (str); number --> object 2 (int). object 1: '3'; object 2: 3
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python Type Conversions". Teaching focus: A conversion asks Python for a value of another type. The text must have a suitable format; int("three") cannot produce an integer. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS text = '3' number = int(text) print(number + 1) | v DISPLAYED RESULT 4 FINAL BINDINGS text --> object 1 (str) number --> object 2 (int) object 1: '3' object 2: 3 Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python Type Conversions. Instructions: text = '3' number = int(text) print(number + 1). Displayed result: 4. The final example bindings are text --> object 1 (str); number --> object 2 (int). object 1: '3'; object 2: 3

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
text = '3'
number = int(text)
print(number + 1)
Live state

Top-level scope

text→Object 1
number→Object 2
Object 1 · str'3'Object 2 · int3
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
4

What do you expect this program to print?

What does this example display?

text = '3'
number = int(text)
print(number + 1)
What does this example display?
Choose one answer. You can try again.

Answer and explanation

4

Follow the printed values in order. The output is: 4

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

A conversion asks Python for a value of another type. The text must have a suitable format; int("three") cannot produce an integer.

A conversion asks Python for a value of another type. The text must have a suitable format; int("three") cannot produce an integer.

Type Conversion

In Python, data can exist in different forms — like strings, integers, floats, or booleans. Sometimes, you may need to convert one type into another. This process is called type conversion or type casting.

Types of Type Conversion

Python supports two main types of conversions:

  1. Implicit Type Conversion – automatically handled by Python
  2. Explicit Type Conversion – done manually by the programmer

Implicit Type Conversion

Python automatically promotes compatible data types during operations. For example, it promotes an int to a float when needed.

x = 5
y = 2.0
result = x + y
print(result)
print(type(result))
7.0
<class 'float'>

In the example above, x is an integer and y is a floating-point number. When added together, Python automatically converts x to a float, so the result is also a float: 7.0. The type() function confirms this by outputting <class 'float'>.

Explicit Type Conversion

This is where you take control using Python’s built-in conversion functions. Let’s explore them one by one.

FunctionDescription
int()Converts to integer
float()Converts to float
bool()Converts to boolean
str()Converts to string

Convert String to Integer

s = "123"
num = int(s)
print(num)
print(type(num))
123
<class 'int'>

In this example, the string "123" is successfully converted into the integer 123. The type() function confirms that num is now an int, not a str.

What If the String Is Not a Number?

Be careful — if the string is not a valid integer, Python throws a ValueError.

s = "abc"
num = int(s)  # This will crash
ValueError: invalid literal for int() with base 10: 'abc'

Convert String to Float

s = "12.34"
f = float(s)
print(f)
print(type(f))
12.34
<class 'float'>

Convert String to Boolean

In Python, any non-empty string is considered True. An empty string is False.

print(bool("hello"))   # True
print(bool(""))        # False
True
False

Convert Number to String

Converting a number to string is often needed while building messages, filenames, or storing data.

age = 25
text = "I am " + str(age) + " years old"
print(text)
I am 25 years old

Convert Float to Integer

When you convert a float to an int, it chops off everything after the decimal point. It doesn’t round — it truncates.

value = 3.99
print(int(value))
3

Using type() to Verify

Always verify what type you are working with using type(). It helps avoid confusion and bugs.

x = "123"
print(type(x))       # Before conversion
x = int(x)
print(type(x))       # After conversion
<class 'str'>
<class 'int'>

Why Do We Need Type Conversion?

Imagine asking Python to add a number and a string — it won’t make sense unless both are of the same type. Type conversion allows us to:

  • Handle user input correctly (which is always a string)
  • Perform mathematical operations
  • Clean and process data
  • Work with files, databases, or external APIs

Best Practices

  • Validate your string data before converting to numbers.
  • Use try-except blocks to handle conversion errors.
  • Don’t assume an empty string is None — it’s False in a boolean context, but still a valid string.

What to remember

Connect what you noticed in the example with the idea below.

Remember

A conversion asks Python for a value of another type. The text must have a suitable format; int("three") cannot produce an integer.

Watch out for this idea

int() accepts every word as a valid number.

This is the misconception to avoid.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Python Basic Input and Output →