The built-in input()
function captures the user input from the keyboard. It displays a prompt message to the user and waits for the user to type their response followed by pressing the Enter key:
input()
Signatures Arguments Argument Description prompt
A string to display as a prompt to the user before waiting for input. Return Value
input()
Examples
With a string prompt to ask for the user’s job:
With a string prompt to ask for the user’s age:
input()
Common Use Cases
The most common use cases for the input()
function include:
input()
Real-World Example
Say that you want to write a number-guessing game where the user enters a number to guess a secret random number between 1
and 10
. To get the user input, you can use input()
as shown below:
This example uses input()
to collect the user’s input. When the user guesses the secret number, the else
clause runs, breaking the loop. The final line of code prints the successful guess message.
Tutorial
How to Read User Input From the Keyboard in PythonReading user input from the keyboard is a valuable skill for a Python programmer, and you can create interactive and advanced programs that run on the terminal. In this tutorial, you'll learn how to create robust user input programs, integrating error handling and multiple entries.
For additional information on related topics, take a look at the following resources:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4