PY-101 ยท Strings and input
User input and Lab 01: Calculator
โฑ 8 min ๐ Real Python 3 ๐ฌ Animated โ Completed
โก 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.
name = input("What is your name? ")age = int(input("How old are you? "))print(f"Hi {name}, next year you will be {age + 1}.")
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
What is your name? Priya
How old are you? 29
Hi Priya, next year you will be 30. ๐ฏ Lab 01: Calculator. Read two numbers with input(), convert them with float(), then print all four results in this format, one per line: 12.0 + 4.0 = 16.0 (then -, * and /).
Input one line per input() call
Press Run to see the output. Python runs in your browser. Need a hint?
print(f"{a} + {b} = {a + b}") and the same for -, * and /
- 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.
- The Python idea
text = input("Prompt: ")input() pauses the program, shows a prompt and returns what the user typed, always as a string. - 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: ")) 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().
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: if, elif and else โ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. Forgetint()orfloat()and"5" + "5"gives"55", not 10.
Syntax
text = input("Prompt: ")
number = int(input("Number: ")) Official documentation
- Built-in functions: input() (docs.python.org)