In Post #14, we built a fantastic calculator. However, it had one major limitation: to change the numbers for our calculation, we had to manually edit the code itself. Truly powerful programs can interact with the user, asking for information and responding accordingly. It’s time to break down that wall between the user and our code.
In this post, we will learn about one of Python’s most exciting built-in functions: input()
. This function allows us to pause our program and get direct input from the user, making our scripts truly interactive.
Introducing the input() Function
The input()
function is a simple but powerful tool. When Python encounters this function in a script, it does the following:
- It displays a message (called a “prompt”) to the user in the terminal.
- It temporarily pauses the program’s execution and waits.
- It captures all the characters the user types until they press the Enter key.
- It gives that captured text back to our program as a string.
That last point is crucial and we will explore it in depth in our next post, but for now, let’s focus on getting text from our user.
The Common Pattern: Prompting and Storing
The most common way to use the input()
function is to ask the user a question and store their answer in a variable.
The syntax looks like this:
variable_name = input(“Your prompt to the user: “)
Let’s break it down:
- The text inside the parentheses is the prompt. This is the message displayed to the user, so it should be a clear instruction or question.
- When the user types their response and hits Enter, that text is returned by the function and stored in the
variable_name
using the assignment operator (=
).
Let’s build a classic example: a program that asks for the user’s name and then greets them.
# Ask the user for their name and store it in a variable
user_name = input("Please enter your name: ")
# Create a greeting message using the provided name
greeting = "Hello, " + user_name + "!"
# Print the final, personalized greeting
print(greeting)
When you run this script, you will see this in your terminal:
Please enter your name:
The program is now waiting. If you type Maria
and press Enter, the program will resume and print:
Hello, Maria!
Let’s Build a Simple Mad Libs Program
To further practice, let’s create another fun, interactive program. This one will ask the user for a few words and then place them into a sentence.
print("--- Simple Mad Libs ---")
print("Please provide the following words to build a story:")
# Get various words from the user
noun = input("A noun (a person, place, or thing): ")
verb = input("A verb (an action, past tense): ")
adjective = "An adjective (a describing word): "
# Create the story by combining the strings
story = "The " + adjective + " " + noun + " " + verb + " over the lazy dog."
# Print the final story. The \n creates a blank line for better formatting.
print("\nHere is your story:")
print(story)
Run this script and answer the prompts. The program will take your words and build a unique (and probably silly) sentence for you every time.
What’s Next?
You’ve now crossed a major threshold in programming. Your programs are no longer static scripts; they can communicate with a user, receive information, and react to it. This is the foundation of building real-world applications.
You may have noticed we’ve only worked with text so far. What happens if we try to use input()
to get a number for our calculator from Post #14? You might run into a surprise. As we’ve hinted, input()
has a specific quirk. In Post #16, we will investigate this “gotcha” and learn why the input()
function always gives you a string, and what we need to do about it.
Author

Experienced Cloud & DevOps Engineer with hands-on experience in AWS, GCP, Terraform, Ansible, ELK, Docker, Git, GitLab, Python, PowerShell, Shell, and theoretical knowledge on Azure, Kubernetes & Jenkins. In my free time, I write blogs on ckdbtech.com