Let's learn about a Python built-in function that can get input from a user while our Python program is running.
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'
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'
input functionTo 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.
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.
Sign up for my 5 day email course and learn essential concepts that introductory courses often overlook!
Sign in to your Python Morsels account to track your progress.
Don't have an account yet? Sign up here.
Sign up for my free 5 day email course and learn essential concepts that introductory courses often overlook: iterables, callables, pointers, duck typing, and namespaces. Learn to avoid beginner pitfalls, in less than a week!
Ready to level up? Sign up now to begin your Python journey the right way!