Python Program to Print Hello World

Write a sample Python program to Print Hello World or any custom message. It is a basic or a traditional program to start any programming language. It also helps you to understand the print function.

Sample Python Program to Print Hello World

This Sample program uses the print function and displays the hello world. It is the standard way of printing any information to the user.

print("Hello World")

Using a String format

We can also use the string formatter to print text messages.

print(f"Hello {'World'}")

Using a Variable

In this example, we declared a msg variable and used the print function to display the message.

msg = "Hello World"
print(msg)

Using escape Characters

This example uses the \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 1

Python program to print Hello World using functions

In this program, we created a function.

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)
Sample Python Program to print Hello World 2

Python Program to Print Hello World multiple times using for loop

We can use the for loop or while loop to print the same message for n number of times. For instance, the below code prints Hello World three times.

for i in range(3):
    print("Hello World")

str = input("\nEnter Any Text = ")
for i in range(3):
    print(str)
Python Program to Print Hello World multiple times