Python input() function
Ask the user to type a line, and return it as a string.
Syntax
input(prompt="")
Shows the prompt, waits until the user presses Enter, and returns what they typed as text, without the new line. Convert it with int() or float() before doing maths. (On this page the examples cannot wait for typing, so this one simulates the answers.)
Parameters
| prompt | the text shown before the cursor |
Return value
str: what the user typed
Example
Code
answers = iter(["Sita", "27"]) def input(prompt=""): # a stand-in for typing, used only on this page answer = next(answers) print(prompt + answer) return answer name = input("Name? ") age = int(input("Age? ")) print(f"Hi {name}, next year you will be {age + 1}")
Output
Name? Sita Age? 27 Hi Sita, next year you will be 28
Good to know.
input("Age? ") + 1 is a TypeError: input always returns a string.Related
Learn it step by step in the free lesson Your First Program, with exercises that check your answers.