Input a welcome message and display it
Expt. No.01
Page No.2
Display a welcome message
Aim
To write a Python program that accepts a welcome message from the user and displays it.
Concepts used
Algorithm
- Ask the user to type a welcome message.
- Store it in a variable.
- Display the message.
Program
# Input a welcome message and display it message = input("Enter a welcome message: ")print("Your message is:")print(message)
A marked line continues the line above it. In your file, write it on the same line.
Output
$ python welcome_message.py
Enter a welcome message: Welcome to the Computer Science lab
Your message is:
Welcome to the Computer Science lab
✓ Printed by running this program, not typed by hand.
Result
The program accepted the message as a string and displayed it exactly as it was typed.
Viva voce: what the examiner may ask
01What data type does input() return?
It always returns a string (str), even if the user types a number.
02What is a variable?
A name that refers to a value stored in memory, here the message the user typed.
03How would you display the message on the same line as the heading?
Pass both to one print(): print("Your message is:", message).
Variations for the lab test
- Ask for the user's name too and print "Hello, <name>!" before the message.
- Display the message three times using the
*repetition operator. - Display the message in uppercase.
! Common mistake Writing
print("message") with quotes prints the word message, not the value stored in the variable.