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 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 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)
We can use the for loop or while loop to print the same message for n number of times. For instance, the below program code prints Hello World three times using the for loop. You canreplcat ether same using a while loop as well.
for i in range(3): print("Hello World") str = input("\nEnter Any Text = ") for i in range(3): print(str)