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

PY-101 ยท Strings and input

User input and Lab 01: Calculator

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

10of 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

    input() pauses the program, shows a prompt and returns what the user typed, always as a string. To do maths with what the user typed, convert it first with int() or float(). In this editor, type the answers in the Input box before you press Run.

    • Read text with input()
    • Convert input to numbers
    • Build a small calculator program

    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.

    User input and Lab 01: Calculator, running
    1. name = input("What is your name? ")
    2. age = int(input("How old are you? "))
    3. print(f"Hi {name}, next year you will be {age + 1}.")
    Press โ–ถ to watch Python run this, one line at a time.
    1. The problemSo far your programs say the same thing every time. Real tools ask you something first: a name, an amount, a choice. One function makes your program listen.
    2. The Python ideatext = input("Prompt: ")input() pauses the program, shows a prompt and returns what the user typed, always as a string.
    3. In real lifeCommand-line tools ask questions the same way: git asks for a commit message, installers ask for a folder. Under the hood it is input().

    Start with a question

    So far your programs say the same thing every time. Real tools ask you something first: a name, an amount, a choice. One function makes your program listen.

    Input turns a script into a tool other people can use. The same pattern (read, convert, calculate, print) is behind every form on the web.

    Explain it like I'm 10

    input() makes the program stop and wait for you to type. What you type comes back as text, so convert it before you do maths with it.

    The technical explanation

    input("Prompt: ") always returns a str, even when the user types 42.

    Wrap it to convert: age = int(input("Age: ")). If the user types something that is not a number, int() raises ValueError.

    Here, the editor reads answers from the Input box, one line per input() call, so you can test with different values quickly.

    Syntax

    text = input("Prompt: ")
    number = int(input("Number: "))

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

    Real-world scenario

    Command-line tools ask questions the same way: git asks for a commit message, installers ask for a folder. Under the hood it is input().

    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 type does input() return?
    2. 2. The user types 5. What is input() + input() if they type 5 twice?
    3. 3. Which line reads a whole number correctly?
    4. 4. What happens with int(input()) if the user types "abc"?
    5. 5. What is printed by print(f"{2.0} * {3.0} = {2.0 * 3.0}")?

    Questions people ask

    How do I take integer input in Python?

    Wrap input() in int(): age = int(input("Age: ")). Use float() instead for decimals.

    Why does input() return a string?

    Python cannot know what you plan to do with what the user typed, so it hands you the exact characters as text. Converting is your decision.

    Interview questions

    How this topic comes up in Python interviews. Answer out loud first, then open the model answer.

    beginner How do I take integer input in Python?

    Wrap input() in int(): age = int(input("Age: ")). Use float() instead for decimals.

    beginner Why does input() return a string?

    Python cannot know what you plan to do with what the user typed, so it hands you the exact characters as text. Converting is your decision.

    intermediate What type does input() return?

    str. input() always returns a string.

    intermediate The user types 5. What is input() + input() if they type 5 twice?

    "55". Both are strings, so + joins them into "55".

    Quick revision

    Remember

    • Read text with input()
    • Convert input to numbers
    • Build a small calculator program
    • input() always returns text. Forget int() or float() and "5" + "5" gives "55", not 10.

    Syntax

    text = input("Prompt: ")
    number = int(input("Number: "))

    Official documentation