Let's learn about a Python built-in function that can get input from a user while our Python program is running.
Prompting for user input
Python has a built-in input function that we can use to prompt a user of our program to enter some text:
>>> color = input("Favorite color:")
Favorite color:β―
Our Python REPL is now hanging and waiting for us (the user) to input text.
Let's type purple:
>>> color = input("Favorite color:")
Favorite color:purpleβ―
After the user has typed the text they'd like to enter, they can hit the Enter key to lock-in the value.
>>> color = input("Favorite color:")
Favorite color:purple
>>>
Now the color variable contains the string purple:
>>> color
'purple'
Customizing the prompt text
Note that the input function will print out any string that we give it, so we can make our input prompt as friendly as we like.
For example here we're asking a question and we've put a space at the end of our prompt:
>>> color = input("What's your favorite color? ")
What's your favorite color? yellow
>>> color
'yellow'
Prompt users with Python's built-in input function
To get input from a user while your program is running, you can use Python's built-in input function to put your program on pause and prompt the user for input.
New to Python? Try Python Jumpstart!
Python Jumpstart is designed to help new Python programmers get up to speed quickly. Get hands-on practice with 50 bite-sized modules that build your Python skills step by step.
Have a long line of code? If you don't have brackets or braces on your line yet, you can add parentheses wherever you'd like and put line breaks within them. We call this "implicit line continuation".