Skip to Content

User Input

How to assign variables with user input.

So far, we’ve covered how to assign variables values directly in a Python file. However, we often want a user of a program to enter new information into the program.

How can we do this? As it turns out, another way to assign a value to a variable is through user input.

While we output a variable’s value using print(), we assign information to a variable using input(). The input() function requires a prompt message, which it will print out for the user before they enter the new information. For example:

likes_snakes = input("Do you like snakes? ")

In the example above, the following would occur:

  1. The program would print “Do you like snakes? “ for the user.
  2. The user would enter an answer (e.g., “Yes! I have seven pythons as pets!”).
  3. The variable likes_snakes would be assigned a value of the user’s answer (in this case, “Yes! I have seven pythons as pets!”).

Try constructing a statement to collect user input on your own!

Ready to Learn More?

Find the course that's right for you! Explore our catalog or get a recommendation.