Please enable JavaScript to use CodeHS

User Input in Python

Learn how to accept user input in your Python programs.

By Rachel Devaney

An important aspect of computer science is creating interactive programs for users. A large part of user interaction is allowing users to input information into a program.

In Python, we use the input() function to ask the user for input. As a parameter, we input the text we want to display to the user. Once the user presses “enter,” the input value is returned. We typically store user input in a variable so that we can use the information in our program.

Take a look at the following program to get a feel for how user input works. Run the program and explore with these guiding questions:

  • What do you notice about the type of value input() returns?
  • Why is it important to store the input the user gives in a variable?

Data Type Conversion

You should have noted that input() returns the user input as a string, even if the user inputs a number! This can cause complications if you’re wanting to do any calculating with the number. Luckily, you can easily convert strings to integers in Python by using the int() function. This is also known as casting, which you can learn more about at here.

Take a look at the slides to see how you can use both the int() and input() functions to obtain numerical information from the user.


See the int(input()) command in action in the program below, and explore how it works with these guiding questions:

  • What happens when you remove the int() function in line 1? Why does this happen?
  • What happens when you enter a string instead of a number into the input prompt? Why does this happen?

Your Turn!

Now that you know the basics of user input, try it out by completing the following exercises. In each program, you should use variables to store the information gathered from the user.

  1. Ask the user for their age. Then print out how old they will be in 10 years.
  2. Ask the user for their name. Then print out three greeting statements in three different languages.
  3. Ask the user for two numbers. Then calculate and print out the sum.