Sample 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.

print("Hello World")


Hello World

Using a String format

print(f"Hello {'World'}")
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"
Hello World

Using escape Characters

This example uses the \t for the tab space and \n for a new line in between the hello and world.


Hello	World

Python program to print hello world using functions

In this program, we created a function.

def message():
    print("Hello World")

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")
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 print Hello World three times.

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

About Suresh

Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.