Python Tutorials · Python Basics

Python User Input

Learn all about Python User Input in this comprehensive tutorial.

5 min read beginner
  • Python allows for user input.
  • In the example above, the user had to input their name on a new line.
  • You can add as many inputs as you want, Python will stop executing at each of them, waiting for user input:
  • The input from the user is treated as a string.
  • It is a good practice to validate any input from the user.

User Input

Python allows for user input.

That means we are able to ask the user for input.

The following example asks for your name, and when you enter a name, it gets printed on the screen:

python
Note: Python stops executing when it comes to the input() function, and continues when the user has given some input.

Using prompt

In the example above, the user had to input their name on a new line. The Python input() function has a prompt parameter, which acts as a message you can put in front of the user input, on the same line:

python

Multiple Inputs

You can add as many inputs as you want, Python will stop executing at each of them, waiting for user input:

python

Input Number

The input from the user is treated as a string. Even if, in the example above, you can input a number, the Python interpreter will still treat it as a string.

You can convert the input into a number with the float() function:

python

Validate Input

It is a good practice to validate any input from the user. In the example above, an error will occur if the user inputs something other than a number.

To avoid getting an error, we can test the input, and if it is not a number, the user could get a message like "Wrong input, please try again", and allowed to make a new input:

python

Module quiz

2 questions
1

Which of the following is true about Python User Input?

2

What is the most common pitfall when working with Python User Input?

Answer all questions to submit.