Thanks to visit codestin.com
Credit goes to www.pythonmorsels.com

Prompting a user for input

Share
Copied to clipboard.
Trey Hunner smiling in a t-shirt against a yellow wall
Trey Hunner
1 min. read Watch as video Python 3.10—3.14
Tags

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.

5 Keys to Python Success 🔑

Sign up for my 5 day email course and learn essential concepts that introductory courses often overlook!