PY-101 ยท Variables and data types
Type conversion
โฑ 9 min ๐ Real Python 3 ๐ฌ Animated โ Completed
โก The 30-second answer
Type conversion, also called casting, turns a value into another type with int(), float(), str() or bool(). You need it whenever text must become a number, for example input from a user. int() drops the decimal part instead of rounding.
- Convert text to numbers and back
- Know that int() truncates
- Know which values bool() treats as False
Try it
Watch it come alive, run it yourself, then prove it. Real Python, right on this page.
Watch Python run the example. Every variable is a jar; watch the values change and the output appear.
a = "5"b = "7"print(a + b)print(int(a) + int(b))print(int(3.9))print(float("2.5") * 2)print(str(100) + " percent")print(bool(0), bool(""), bool("hi"))
Change anything and press Run (or Ctrl+Enter). Then open Watch it and press Watch my code.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Expected output
57
12
3
5.0
100 percent
False False True ๐ฏ qty and price arrive as text. Convert them, store qty times price in total, and print total rounded to 2 decimal places. The output should be 59.97.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Need a hint?
total = int(qty) * float(price)
- The problemYou read a price from a file: "19.99". You multiply it by 3 and get "19.9919.9919.99". The price was text all along.
- The Python idea
int(x) float(x) str(x) bool(x)Type conversion, also called casting, turns a value into another type with int(), float(), str() or bool(). - In real lifeA CSV export of orders has quantity "3" and price "19.99" as text. A report that forgets to convert them will join the strings instead of multiplying the numbers.
Start with a question
You read a price from a file: "19.99". You multiply it by 3 and get "19.9919.9919.99". The price was text all along.
Data arrives as text: from input(), files, web forms and APIs. You have to convert it before you can do maths with it.
Explain it like I'm 10
Conversion translates a value into another kind. int("5") turns the text 5 into the number 5, and now you can do maths with it.
The technical explanation
"5" + "7" joins two strings and gives "57". int("5") + int("7") adds two numbers and gives 12.
int(3.9) is 3. It cuts off the decimal part. Use round(3.9) if you want 4.
int("hello") raises a ValueError because the text is not a number. Converting user input can fail, and later courses show how to handle that.
bool() returns False for 0, 0.0, "", None and empty collections. Everything else is True.
Syntax
int(x) float(x) str(x) bool(x) Real-world scenario
A CSV export of orders has quantity "3" and price "19.99" as text. A report that forgets to convert them will join the strings instead of multiplying the numbers.
Test yourself
5 questions. Get 4 right and this lesson is marked complete.
Pass the quiz to complete this lesson.
๐ Every lesson counts toward your certificate Next: Arithmetic operators โQuestions people ask
How do I convert a string to an integer in Python?
Use int(), for example int("42") returns 42. If the text might not be a number, check it first or catch ValueError.
Does int() round in Python?
No. int() drops the decimal part, so int(2.9) is 2 and int(-2.9) is -2. Use round() to round to the nearest whole number.
Interview questions
How this topic comes up in Python interviews. Answer out loud first, then open the model answer.
beginner How do I convert a string to an integer in Python?
Use int(), for example int("42") returns 42. If the text might not be a number, check it first or catch ValueError.
beginner Does int() round in Python?
No. int() drops the decimal part, so int(2.9) is 2 and int(-2.9) is -2. Use round() to round to the nearest whole number.
intermediate What is "3" + "4"?
"34". + joins strings, so the result is the string "34".
intermediate What is int(9.99)?
9. int() truncates toward zero, it does not round.
Quick revision
Remember
- Convert text to numbers and back
- Know that int() truncates
- Know which values bool() treats as False
int()cuts off the decimals, it does not round:int(2.99)is 2. Useround()when you want the nearest whole number.
Syntax
int(x) float(x) str(x) bool(x) Official documentation
- Built-in functions: int() (docs.python.org)
- Truth value testing (docs.python.org)