Create your own
Lesson illustration

Build and Run a Personalized Greeting Program

Welcome back. In the previous lesson, you used print() to display text, added comments with #, and ran a short script in VS Code. You will now turn those pieces into a program that responds to the person running it: it will ask for a name, then display a greeting using that name.

By the end of this lesson, you will have saved, run, tested, and personalized a greeting.py program. This is the last outcome in the setup module; the next module will examine values, variables, and user input more systematically.


From a fixed message to a personalized program

A program such as this always displays the same text:

print("Hello, Sam!")

That is fine when the message is meant for one specific person. But it is not very flexible. To greet whoever runs the program, the program needs to:

  1. Ask the person to type their name.
  2. Remember the text they typed.
  3. Use that text in a message.

Python’s input() function handles the first two steps. It displays a prompt, pauses the program, and waits for the user to type something and press Enter.

name = input("What is your name? ")

Read this line from right to left:

  • input("What is your name? ") displays the prompt and collects the typed response.
  • name = stores that response under the label name.

A label that stores a value is called a variable. For now, treat name as a named place where your program keeps the person’s answer so it can use it later.

This short video shows the complete idea: collecting a name and using it in output.

Python user input ⌨️

Watch “Python user input” by Bro Code for a quick visual demonstration of a program pausing for a name, storing it, and printing a greeting.

Watch capture a name to see the prompt and assignment to name. Then watch use the name to see how the stored text becomes part of a greeting. Notice that the program does not continue until the user presses Enter.

Here is the crucial distinction:

print("Hello, name!")

This prints the literal characters name because quotation marks tell Python, “This is ordinary text.”

print("Hello, " + name + "!")

This uses the variable name because it has no quotation marks around it. The + signs join the greeting text, the stored name, and the exclamation mark into one string.

For example, if the user enters Avery, the output becomes:

Hello, Avery!

Read the two-line pattern

Before building, read the worked greeter.py example in the following resource. It uses a slightly different—but equally valid—style of giving several pieces to print().

Basic Input and Output in Python

Read the “Combining Python Input and Output in a Practical Example” section from Real Python. It presents the same program structure you are about to build: prompt for a name, store the response, and print a tailored greeting.

In the section “Combining Python Input and Output in a Practical Example,” read from the introductory paragraph through the explanation just before the collapsible guess-the-number exercise. Start with the setup rationale, but also study the two-line greeter.py code block immediately below it. Focus on the roles of input(), name, and print(), rather than the later game exercise.

The resource’s version is:

name = input("Please enter your name: ")
print("Hello", name, "and welcome!")

Its print() call accepts several values, puts a space between them, and displays them. Your version below will use + to assemble one complete message. Both approaches are valid; learning that Python often offers more than one readable solution is useful.


Build your greeting program

In VS Code, open your existing Python project folder and create a file named:

greeting.py

Enter this code exactly first:

# A personalized greeting program

name = input("What is your name? ")
print("Hello, " + name + "!")
print("Welcome to my Python program.")

Then save the file with Ctrl+S.

The comment is for people reading the file; Python ignores it when running the program. The remaining lines execute from top to bottom:

  1. The program asks for a name and waits.
  2. It stores the answer in name.
  3. It prints the custom greeting.
  4. It prints a fixed welcome message.
A Python code snippet shows comments beginning with `#` above and below a `print()` call, illustrating that comments explain code for readers while `print()` produces visible output.

Run it in VS Code

Use the Run Python File in Terminal button near the top-right of the VS Code editor. A terminal panel should open at the bottom.

You should see a prompt similar to:

What is your name?

Click in the terminal if necessary, type your name without quotation marks, and press Enter. For example:

What is your name? Jordan
Hello, Jordan!
Welcome to my Python program.

The name appears on the same line as the prompt because input() waits for your typing there. That is normal.

If you prefer the right-click method, right-click in the editor and choose Run > Python File in Terminal. Both methods run the saved greeting.py file.

A successful run means all of the following happened:

  • The program displayed the name prompt.
  • You could type a name and press Enter.
  • The greeting included the name you typed.
  • The terminal returned to a prompt after the final message, meaning the script finished.

Test, personalize, and observe

Run greeting.py at least three times, entering a different name each time. You should not need to edit the code between runs. The changing output demonstrates why input() is useful.

Next, make the program feel like yours. Keep the input() line and greeting line, but revise the final message and add one more output line. For example:

# A personalized greeting program

name = input("What is your name? ")
print("Hello, " + name + "!")
print("I am learning to build useful programs with Python.")
print("Thanks for trying my greeting program.")

You can also improve the prompt:

name = input("Please type your first name: ")

A good prompt tells the user exactly what the program expects.

After every small change, follow the same programming cycle:

  1. Save with Ctrl+S.
  2. Run the file.
  3. Enter a name.
  4. Check whether the output matches what you intended.

This is more reliable than making many edits and trying to diagnose all of them at once.

A few common mistakes

What you seeLikely causeFix
Hello, name!You wrote "name" inside quotation marks.Use name without quotation marks.
A SyntaxError mentioning a stringA quote is missing or mismatched.Make sure each opening " has a closing ".
A NameError involving nameThe variable was misspelled, perhaps as Name or nme.Use exactly name in both lines. Python is case-sensitive.
The program seems to stop at the questionIt is waiting for input.Type a name in the terminal and press Enter.
Your new message does not appearThe file may not have been saved before running.Save, then run again.

Try entering a blank response once by pressing Enter without typing a name. The program will still run, but the greeting will look incomplete. Later, when you learn decisions and loops, you will make programs request a valid answer instead of accepting an empty one.


Key takeaways

A personalized greeting program combines a few simple instructions into a useful interaction:

  • input() displays a prompt and waits for text from the keyboard.
  • name = ... stores that text in a variable named name.
  • A variable name is written without quotation marks when you want Python to use its stored value.
  • print("Hello, " + name + "!") joins text with the entered name and displays the result.
  • Save before running, enter text in the terminal, and test with several different names.

You have now completed the first module: your Windows setup can create and run Python programs, and you can produce output that responds to a user. Next, you will look more closely at the values Python works with, how variables store and change them, and how to use input for programs that calculate results.

Can't find a good explanation? Sign up and we'll make it for you

Sign up