ByteBoard Notes Notes

Input a welcome message and display it

Computer Science XI · Experiment 01 of 27 · Python

★ CBSE suggested11-CSPython programs
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

  1. Ask the user to type a welcome message.
  2. Store it in a variable.
  3. 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

  1. Ask for the user's name too and print "Hello, <name>!" before the message.
  2. Display the message three times using the * repetition operator.
  3. Display the message in uppercase.
! Common mistake Writing print("message") with quotes prints the word message, not the value stored in the variable.
Back to the 11-CS index