Lecture

Getting User Input in Python

When creating a program, there are times when you need to gather information from the user. In such cases, the input() function comes into play.

The input function returns data entered by the user through the keyboard as a string (text data).


How is it used?

The input() function displays a message to the user and waits for the user's input.

Once the user presses the enter key, the input process ends, and the entered content is returned as a string.

Using the input() function
user_input = input("Please enter your name:") # Displays a message to the user and waits for the user's input print("Welcome", user_input) # Outputs "Welcome [user input]"

When is it typically used?

The input() function is used when you need to gather settings, information, or data from the user that is required for the program to operate.

Example using the input function
name = input("Please enter your name: ") hobby = input("Please enter your hobby: ") print(name + "'s hobby is " + hobby + ".")

Precautions

The input function always returns the data received from the user as a string (text data).

If you need a number instead of text, wrap the input() function with int() to convert the response.

Receiving Numeric Input
user_input = input("Please enter a number: ") number = int(user_input) print(number)

Coding Practice

Try using Python's input() function to ask the user a question and respond based on their answer.

Click the green Run button to see how the input works in action.

Lecture

AI Tutor

Design

Upload

Notes

Favorites

Help

Code Editor

Run
Generate

Execution Result