Remember
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.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
text = '3'
number = int(text)
print(number + 1)Top-level scope
'3'Object 2 · int3A conversion asks Python for a value of another type. The text must have a suitable format; int("three") cannot produce an integer.
(no output)
What do you expect this program to print?
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: 3Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
text = '3'
number = int(text)
print(number + 1)Top-level scope
'3'Object 2 · int3The example has finished. Compare the final output with the values and references above.
4
What do you expect this program to print?
text = '3'
number = int(text)
print(number + 1)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.
Python supports two main types of conversions:
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'>.
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.
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'
s = "12.34"
f = float(s)
print(f)
print(type(f))
12.34
<class 'float'>
In Python, any non-empty string is considered True. An empty string is False.
print(bool("hello")) # True
print(bool("")) # False
True
False
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
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
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'>
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:
None — it’s False in a boolean context, but still a valid string.Connect what you noticed in the example with the idea below.
A conversion asks Python for a value of another type. The text must have a suitable format; int("three") cannot produce an integer.
int() accepts every word as a valid number.
This is the misconception to avoid.Reaching the recap and completing the quizzes are separate milestones.