Python Program to Ask User Name and Display Welcome Message
When learning programming, one of the first concepts students learn is taking input from the user. Programs become useful only when they can interact with users and process their input. Python makes user interaction very simple through the use of the input() function.
In this tutorial, we will learn how to write a Python program that asks the user to enter their name and then prints a welcome message.
Problem Statement
Write a Python program that prompts the user to enter their name and then displays a greeting message.
When the user enters the name Sarah, the output should look like this:
Enter your name: Sarah
Hello Sarah
This example demonstrates how Python programs can take input from users and display customized output.
Python Program
Below is the Python code for this task:
# The code below almost works
name = input('Enter your name')
print('Hello', name)
This is a very simple program, but it introduces an important programming concept: user input.
Let us understand the code step by step.
Understanding the Code
1. Comment Line
# The code below almost works
In Python, the # symbol is used to write comments. Comments are explanatory notes written by programmers to describe what the code does.
Comments are ignored by the Python interpreter and do not affect program execution. They are used only to improve code readability and documentation.
For example:
# Ask the user for their name
This helps other programmers understand the purpose of the program.
2. Using the input() Function
name = input('Enter your name')
The input() function is used to take input from the user.
When this line executes:
-
Python displays the message Enter your name.
-
The program waits for the user to type something.
-
The value entered by the user is stored in the variable
name.
For example, if the user types:
Sarah
Then the variable name will store:
"Sarah"
This allows the program to use the user’s input later in the program.
3. Printing the Output
print('Hello', name)
The print() function is used to display output on the screen.
In this line, two values are printed:
-
The text
"Hello" -
The value stored in the variable
name
If the user entered Sarah, the output will be:
Hello Sarah
Python automatically adds a space between the two values when printing them.
Example Execution
Here is how the program works when it runs:
Step 1: The program displays a message asking for input.
Enter your name
Step 2: The user types their name.
Sarah
Step 3: The program prints the greeting message.
Hello Sarah
This simple interaction shows how Python programs communicate with users.
Why User Input is Important
User input is a fundamental concept in programming. Most real-world programs require data from users in order to perform tasks.
For example:
-
Login systems ask for username and password
-
Online forms ask for personal information
-
Calculators ask for numbers to perform calculations
-
Games ask for player choices
Without user input, programs would not be interactive.
Improving the Program
We can slightly improve the program by adding punctuation and making the greeting more friendly.
Example:
name = input("Enter your name: ")
print("Hello", name, "! Welcome to Python programming.")
Example output:
Enter your name: Sarah
Hello Sarah ! Welcome to Python programming.
This makes the program more user-friendly.
Using f-Strings (Modern Python Method)
Python also provides a modern method called f-strings to format output.
Example:
name = input("Enter your name: ")
print(f"Hello {name}, welcome!")
Output:
Enter your name: Sarah
Hello Sarah, welcome!
This method is commonly used in modern Python programming because it is clean and easy to read.
Conclusion
This simple Python program demonstrates how to take input from the user and display a customized message. The input() function allows programs to collect data from users, while the print() function is used to display output.
Even though the program is small, it introduces an essential programming concept: user interaction. Understanding how to take input and display output is the foundation for building more advanced programs.
As you continue learning Python, you will use user input in many applications such as calculators, data processing programs, games, and web applications. Mastering this basic concept is an important step in becoming a confident Python programmer.
0 Comments