Python TutorialLesson 35Free preview
Python User Input
This lesson is part of Python · All levels
Summary
The input() function reads text from the user via the keyboard and always returns string data. Therefore, the input must be converted to int() or float() if it is to be used as a number. You can also type prompt text in input() so that the user knows what to type.
Key points
- It displays the prompt, waits for Enter, then returns the string.
- For numbers use int(input(...)) or float(input(...)).
- Conversion could fail with ValueError, so secure it with try/except when input is uncertain.
- In Python 3 is sufficient; the function is only present in Python 2.
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.