Lesson 6 of 60
โฎPrev Nextโฏ

PY-101 ยท Variables and data types

Type conversion

โฑ 9 min ๐Ÿ Real Python 3 ๐ŸŽฌ Animated

6of 60

Your note

My notes for this lesson

Select any text in the lesson to highlight it or add a note. Saved in this browser.

    No highlights yet. Select a sentence in the lesson, then pick a colour or add a note.

    All my bookmarks and notes

    Reading settings

    For every lesson, saved in this browser.

    Text size
    Line spacing
    Font

    With IaaS you rent the virtual machine. Microsoft looks after the building, power and hardware; you patch the operating system.

    โšก 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.

    Type conversion, running
    1. a = "5"
    2. b = "7"
    3. print(a + b)
    4. print(int(a) + int(b))
    5. print(int(3.9))
    6. print(float("2.5") * 2)
    7. print(str(100) + " percent")
    8. print(bool(0), bool(""), bool("hi"))
    Press โ–ถ to watch Python run this, one line at a time.
    1. 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.
    2. The Python ideaint(x) float(x) str(x) bool(x)Type conversion, also called casting, turns a value into another type with int(), float(), str() or bool().
    3. 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)

    โ–ถ Run the example ๐ŸŽฌ Watch it come alive

    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.

    Ready for more?The full lesson adds the technical explanation, interview questions and quick revision.

    Test yourself

    5 questions. Get 4 right and this lesson is marked complete.

    1. 1. What is "3" + "4"?
    2. 2. What is int(9.99)?
    3. 3. What happens with int("ten")?
    4. 4. Which value is False when passed to bool()?
    5. 5. What does str(5) + "5" give?

    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. Use round() when you want the nearest whole number.

    Syntax

    int(x)   float(x)   str(x)   bool(x)

    Official documentation