Python Type Conversion
Digits inside quotes are still text. int("2") + int("3") is the number 5. "2" + "3" is the text 23. int turns a text made of digits into a number. It does not change a name until you store the result.
print(int("2") + int("3"))
print("2" + "3")
The first line is 5. The second line is 23.
The other direction is text
str(2) turns the number 2 into the text 2. str(2) + str(3) joins them, so the result is 23. 2 + 3 stays a sum, so the result is 5. str does not change the stored number. It makes a text you can join.
print(str(2) + str(3))
print(2 + 3)
The first line is 23. The second line is 5.
A stored text of digits
cups = "3" stores the text 3, the same kind of text input() gives you. This page does not call input(). int(cups) + 2 reads that text as a number and adds 2, so the result is 5. cups itself stays the text 3 until you store int(cups) back into the name.
cups = "3"
print(int(cups) + 2)
The line is 5.
A letter stops, and the fraction is cut off
int("lida") stops the program. Those characters are not digits. int("2.5") also stops: the point is not part of a whole number written as text. float("2.5") reads it as 2.5. int(2.9) does not round. It cuts off the fraction and leaves 2. None is not text and not a number. This page does not convert it.
Four calls, and what each one gives you when it runs.
📊 What the call gives you
| Call | Result |
|---|---|
| int("2") | the number 2 |
| str(2) | the text 2 |
| float("2.5") | 2.5 |
| int(2.9) | 2 |
The next page is the arithmetic signs
This page does not choose a branch, compare two values, or join two conditions. The next page is the signs that add, subtract, multiply, and divide. Here the plus sign only showed the difference between a sum and joined text.
Say these yourself
- int("2") + int("3") is 5. "2" + "3" is 23.
- str(2) + str(3) is 23. 2 + 3 is 5.
- int("3") + 2 is 5. The stored text stays text until you store the number.
- int("lida") stops the program.
- int(2.9) is 2. The fraction is cut off, not rounded.
- The next page is the signs that add, subtract, multiply, and divide.
Easy to misread
- Expecting "2" + "3" to be 5
- Expecting int("2") + int("3") to be 23
- Expecting int("lida") or int("2.5") to run
- Reading int(2.9) as 3
- Thinking int changes the stored text by itself
Keep
int turns digit text into a number. str turns a number into text. float reads a point in text.
int("2") + int("3") is 5. "2" + "3" is 23. str(2) + str(3) is 23.
A letter stops the program. int("2.5") stops. float("2.5") is 2.5.
int(2.9) is 2. The fraction is cut off.
The name keeps the old text until you store the new value.
The next page is the signs that add, subtract, multiply, and divide.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top