input() prompts for and reads one line from standard input, returning the entered text as a string. The prompt is optional and may be any string, including an f-string. section 4.9 section 13.6
Use input() with these practical rules:
- Pressing Enter terminates the input line, and input() removes the trailing newline. section 13.6
- Convert numeric text explicitly with int() or float() before using it as a number. section 4.9
- Expect an exception when the entered text cannot be converted to the requested numeric type. section 13.6
- Use sys.stdin for lower-level reading such as read(), readline(), and readlines(), especially when redirecting input. section 13.6
Read text and numbers
name = input("Name? ")\nprint(name)\n\nage = int(input("Age? "))\nprint(age)\n\nA text response remains a string; wrapping input() with int() converts numeric text to an integer. Replace int() with float() when decimal input is required.
name = input("Name? ")
Name? Jane
print(name)
Jane
age = int(input("Age? ")) #1
Age? 28
print(age)
28
#1 Converts input from a string to an int
View the prompt, entered values, printed results, and the inline conversion annotation in one compact interaction. section 4.9






