“Input a welcome message and display it” is usually the first program that takes data from the user. It teaches the two most basic actions in every program, taking input and giving output, and it introduces your first variable. In this post you will see the program in Python, C and Java, a step-by-step explanation, nine other ways to write it, and answers to common questions. Use the interactive tool below to type your own message and watch it travel from the keyboard to the screen.
Type a message and watch it travel from the keyboard into a variable and then onto the screen.
Graphical view
Code
Output
What You Will Learn
- The problem statement
- How input and output work
- The program in Python, C and Java
- Step-by-step explanation
- Other ways: f-strings, format(), upper(), word count and more
- Common mistakes, practice questions and FAQ
Problem Statement
Input a welcome message and display it.
The user types a welcome message, for example Welcome to my website, and presses Enter. The program stores it and prints it back on the screen. The interactive tool above also has a mode that shows the length of the message and a mode that joins the message with a name.
How Input and Output Work
Every program has three parts: input (data coming in), processing (what the program does with it) and output (the result going out). Here the processing is almost nothing, which makes it a perfect first example.
- Input:
input("Enter welcome message: ")shows the prompt, waits for the user to type a line and press Enter, and returns that line as a string (text). - Variable:
message = ...gives the text a name, so that the program can use it later. Think of a variable as a labelled box in memory. - Output:
print(message)sends the content of the variable to the screen.
A string is stored as a row of characters, and each character has a position called an index, starting from 0. In the message “Hello” the letter H is at index 0 and o is at index 4. The interactive tool shows this row of boxes in the memory stage.
The Program
Logic:
- Ask the user for the message and store it in the variable
message. - Print the variable.
Python program
message = input("Enter welcome message: ")
print(message)
Output
Enter welcome message: Welcome to my website
Welcome to my website
C program
#include <stdio.h>
int main() {
char message[100];
printf("Enter welcome message: ");
scanf(" %99[^\n]", message);
printf("%s\n", message);
return 0;
}
Enter welcome message: Welcome to my website
Welcome to my website
C has no string type, so the message is stored in a char array of 100 places. The format %99[^\n] in scanf means “read up to 99 characters, stopping at the end of the line”. It is used because a plain %s stops at the first space and would read only “Welcome”. The space before the % skips any leftover newline. Printing uses %s for a string.
Java program
import java.util.Scanner;
public class WelcomeMessage {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter welcome message: ");
String message = sc.nextLine();
System.out.println(message);
}
}
Enter welcome message: Welcome to my website
Welcome to my website
Save this file as WelcomeMessage.java, because the public class name must match the file name. sc.nextLine() reads the whole line including spaces, while sc.next() would read only one word.
Step-by-Step Explanation
Suppose the user types Welcome to my website.
| Step | Statement | What happens | message | Screen |
|---|---|---|---|---|
| 1 | input(…) | The prompt appears and the user types a line | Enter welcome message: | |
| 2 | message = … | The typed line is stored in the variable | “Welcome to my website” | |
| 3 | print(message) | The content of the variable is copied to the screen | “Welcome to my website” | Welcome to my website |
The message has 21 characters, counting the three spaces. To count them in a program, use len():
message = input("Enter welcome message: ")
print(message)
length = len(message)
print("Length:", length)
Enter welcome message: Hello, World!
Hello, World!
Length: 13
“Hello, World!” has 13 characters: 10 letters and digits, the comma, the space and the exclamation mark. Every character counts, including spaces and punctuation.
Other Ways to Write the Program
Method 2: Join the message and a name with +
The + operator joins strings. This is called concatenation. Both values must be strings, so you cannot add a number to a string without converting it first.
name = input("Enter your name: ")
message = input("Enter welcome message: ")
print(message + ", " + name + "!")
Enter your name: Riya
Enter welcome message: Welcome to my website
Welcome to my website, Riya!
Method 3: f-string
An f-string puts the variables directly inside the text, between curly brackets. It is the cleanest way to build a message in modern Python.
name = input("Enter your name: ")
message = input("Enter welcome message: ")
print(f"{message}, {name}!")
Method 4: format()
The older format() method fills each {} with a value in order.
name = input("Enter your name: ")
message = input("Enter welcome message: ")
print("{}, {}!".format(message, name))
Method 5: print() with commas
If you give print() several values separated by commas, it prints them with a space between. There is no need to join them yourself.
name = input("Enter your name: ")
message = input("Enter welcome message: ")
print(message, name)
Enter your name: Riya
Enter welcome message: Welcome
Welcome Riya
Method 6: A fixed message without input
The simplest welcome program has no input at all. The message is written directly in the program. It is useful for testing, but it always prints the same text.
print("Welcome to my website")
Welcome to my website
Method 7: Change the case with upper(), lower() and title()
Strings have many ready-made methods. These three return a new string and leave the original unchanged.
message = input("Enter welcome message: ")
print(message.upper())
print(message.lower())
print(message.title())
Enter welcome message: welcome to my website
WELCOME TO MY WEBSITE
welcome to my website
Welcome To My Website
Method 8: Count the words
split() cuts the message at the spaces and returns a list of words. len() of that list is the number of words.
message = input("Enter welcome message: ")
words = message.split()
print("Words:", len(words))
Enter welcome message: Welcome to my website
Words: 4
Method 9: Display the message several times
message = input("Enter welcome message: ")
n = int(input("How many times? "))
for i in range(n):
print(message)
Enter welcome message: Hi!
How many times? 3
Hi!
Hi!
Hi!
Special characters: \n and \t
Inside a string, \n starts a new line and \t inserts a tab.
print("Welcome\tto\nmy website")
Welcome to
my website
C and Java: message with a name
C
#include <stdio.h>
int main() {
char name[50];
char message[100];
printf("Enter your name: ");
scanf(" %49[^\n]", name);
printf("Enter welcome message: ");
scanf(" %99[^\n]", message);
printf("%s, %s!\n", message, name);
return 0;
}
Java uses printf with %s and %n (a new line):
import java.util.Scanner;
public class WelcomeName {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = sc.nextLine();
System.out.print("Enter welcome message: ");
String message = sc.nextLine();
System.out.printf("%s, %s!%n", message, name);
}
}
Save the Java program as WelcomeName.java.
Common Mistakes
- Putting the variable name in quotes.
print("message")prints the word message, not its content. Quotes make something text; without quotes Python looks for a variable.
message = input("Enter welcome message: ")
print("message")
Enter welcome message: Welcome to my website
message
- Reading only one word in C.
scanf("%s", message)stops at the first space. Use%99[^\n]orfgets()to read the whole line. - Using next() instead of nextLine() in Java.
next()returns one word only.nextLine()returns the whole line. - Forgetting the prompt text. Without a prompt, the program waits silently and the user does not know what to type.
- Adding a number to a string.
"Age: " + 15gives an error in Python. Convert withstr(15), or use an f-string. - Wrong Java file name. A public class must be saved in a file with the same name, such as
WelcomeMessage.java. - Missing quotes or brackets. A missing closing quote or bracket gives a SyntaxError. Check each opening quote has a closing one.
Practice Questions
- Read your name and print “Hello, name! Welcome to Class 11”.
- Read a message and print it in capital letters.
- Read a message and print the first and the last character. Hint: message[0] and message[-1].
- Read a message and print how many spaces it contains. Hint: message.count(” “).
- Read a message and print it inside a box of stars.
- Read a first name and a last name in two inputs, and print the full name in one line.
Frequently Asked Questions
How do you take a welcome message as input in Python?
Use message = input("Enter welcome message: "). The text in brackets is the prompt, and whatever the user types is stored in the variable message as a string.
How do you display the message?
Use print(message). Do not put quotes around the variable name, or Python will print the word itself.
What does input() return?
Always a string, even if the user types digits. To use the value as a number, convert it with int() or float().
Why does my C program print only the first word?
scanf("%s", ...) stops reading at a space. Use scanf(" %99[^\n]", message) or fgets(message, 100, stdin) to read the full line with spaces.
What is a variable?
A variable is a named place in memory that stores a value. In message = input(...) the name is message and the value is the text the user typed. You can read it as many times as you want, and you can change it later.
What is string concatenation?
It means joining strings end to end with the + operator, such as "Hello, " + name. Python also offers f-strings and format() for the same job.
How do I print a message on a new line or with a tab?
Put \n for a new line or \t for a tab inside the string. For example, print("Welcome\nto my website") prints two lines.
What is the difference between print(“Hello”) and print(Hello)?
With quotes, "Hello" is text and is printed as it is. Without quotes, Python treats Hello as a variable name and gives a NameError if no such variable exists.
How many characters can the message have?
In Python there is no practical limit. In C the size of the array limits it, so a char message[100] holds at most 99 characters and the end marker. The interactive tool above allows up to 40 characters.
Can the welcome message contain Hindi or other languages?
Python 3 and Java can store them because they use Unicode. Plain C with char arrays works with English (ASCII) text in school programs, so the interactive tool above accepts English text only.