This article shows how to write a sample Python program to print Hello World or any custom message. It is a basic or traditional program for starting (coding) any programming language. It introduces the basic program structure, work environment, and printing text onto the screen.
Because of its simple and clean syntax, writing a program in Python is straightforward. This Hello World program also helps you to understand the print function.
Sample Python Program to Print Hello World
The following sample program uses the print() function to display Hello World on screen. The print() function is the standard way to print or return information to the user. As our motive is to display the message, this program won’t accept any user input.
Assuming that you have installed Python and the IDE, the steps you must follow to write your first program are as follows:
- Open your favourite IDE. For example, the default IDLE, PyCharm, or any other.
- Create a new file to write the code.
- Please paste the code below into the new file that you have created in your favorite IDE.
- Save the file with a .py extension. For example, hello.py, sample.py, etc.
- Run the program to see Hello World in the console.
TIP: If you are working with the default Python IDLE (Integrated Development and Learning Environment), paste the code after >>>. Here, you don’t have to create a new file (optional).
print("Hello World")
TIP: Since Hello World is a string, we must enclose it within double quotes, single quotes, or triple quotes. Otherwise, the print() function throws an error.
Different ways to write the Hello World program in Python
The simplest way to write a Hello World program in Python is to use the built-in print function. Apart from the above-mentioned program, there are multiple ways to write a Hello World message. This section covers all those methods with an example.
Using a String format
Python 3.6 version introduced f-strings (formatted strings) to format the string messages. This program uses the string formatter to print text messages.
print(f"Hello {'World'}")
If you want, use the + (arithmetic) operator for string concatenation to join the “Hello” and “World” messages.
print('Hello ' + 'World')
Using a Variable
In this Python example, we declared a msg variable to store the Hello World message. Next, used the print function to display the message.
msg = "Hello World"
print(msg)
Using escape Characters
This example uses \t for the tab space and \n for a new line between the hello and world.
print("Hello\tWorld")
print("Hello\nWorld")

Python program to print Hello World using function
We can also create a custom function that prints the Hello World message on screen. By creating a function, we can call it multiple times to display this message.
In this program, we created a custom function called message() using the def keyword. Within the function, we used the print() method to display the Hello World message. Whenever needed, call the message() function to display Hello World.
def message():
print("Hello World")
message()
Hello World
Print Custom Message
This sample code allows the user to input their own message. Next, this code prints that message.
str = input("Welcome to Tutorial Gateway")
print(str)
Welcome to Tutorial Gateway
Using the sys module
If you import the Python sys module into the work environment, you can use the write() function to display Hello World! Message as an output.
from sys import stdout
stdout.write('Hello World!')
Hello World!
Print Hello, World in Python 10 times
We can use a for loop or a while loop to print the same message 10 (N) times. For instance, the code below prints “Hello World” 10 times using a for loop. You can replicate the same using a while loop as well.
for _ in range(10):
print("Hello, World")
Hello, World
Hello, World
Hello, World
...
....
Please use the code below to enter your own custom text that prints 100 times. Instead of printing “Hello World”, it prints a custom (user-entered text) 100 times.
str = input("\nEnter Any Text = ")
for _ in range(100):
print(str)
Enter Any Text = Hi
Hi
Hi
Hi
..
..
In the above code, the range() function generates a sequence of numbers from 0 to 99. So, the for loop iterates from 0 to 99 (100 in total). On each iteration, the print statement is executed to display the Hello, World message to the console.