Python - Hello World Program



This tutorial will teach you how to write a simple Hello World program using Python Programming language. This program will make use of Python built-in print() function to print the string.

Hello World Program in Python

Printing "Hello World" is the first program in Python. This program will not take any user input, it will just print text on the output screen. It is used to test if the software needed to compile and run the program has been installed correctly.

Steps

The following are the steps to write a Python program to print Hello World –

  • Step 1: Install Python. Make sure that Python is installed on your system or not. If Python is not installed, then install it from here: https://www.python.org/downloads/
  • Step 2: Choose Text Editor or IDE to write the code.
  • Step 3: Open Text Editor or IDE, create a new file, and write the code to print Hello World.
  • Step 4: Save the file with a file name and extension ".py".
  • Step 5: Compile/Run the program.

Python Program to Print Hello World

# Python code to print "Hello World"
print ("Hello World")

In the above code, we wrote two lines. The first line is the Python comment that will be ignored by the Python interpreter, and the second line is the print() statement that will print the given message ("Hello World") on the output screen.

Output

Hello World

Different Ways to Write and Execute Hello World Program

Using Python Interpreter Command Prompt Mode

It is very easy to display the Hello World message using the Python interpreter. Launch the Python interpreter from a command terminal of your Windows Operating System and issue the print statement from the Python prompt as follows −

Example

PS C:\> python
Python 3.11.2 (tags/v3.11.2:878ead1, Feb 7 2023, 16:38:35) [MSC v.1934 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

>>> print ("Hello World")
Hello World

Similarly, Hello World message is printed on Linux System.

Example

$ python3
Python 3.10.6 (main, Mar 10 2023, 10:55:28) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> print ("Hello World")
Hello World

Using Python Interpreter Script Mode

Python interpreter also works in scripted mode. Open any text editor, enter the following text and save as Hello.py

print ("Hello World")

For Windows OS, open the command prompt terminal (CMD) and run the program as shown below −

C:\>python hello.py

This will display the following output

Hello World

To run the program from Linux terminal

$ python3 hello.py

This will display the following output

Hello World

Using Shebang #! in Linux Scripts

In Linux, you can convert a Python program into a self executable script. The first statement in the code should be a shebang #!. It must contain the path to Python executable. In Linux, Python is installed in /usr/bin directory, and the name of the executable is python3. Hence, we add this statement to hello.py file

#!/usr/bin/python3

print ("Hello World")

You also need to give the file executable permission by using the chmod +x command

$ chmod +x hello.py

Then, you can run the program with following command line −

$ ./hello.py

This will display the following output

Hello World

FAQs

1. Why the first program is called Hello World?

It is just a simple program to test the basic syntax and compiler/interpreter configuration of Python programming language.

2. Installation of Python is required to run Hello World program?

Yes. Python installation is required to run Hello World program.

3. How do I run a Python program without installing it?

TutorialsPoint developed an online environment where you can run your codes. You can use the Python online compiler to run your Python programs.

4. First Program Vs Hello World Program in Python?

There is no difference. The first program of Python is generally known as the Hello World program.

5. Which is/are the method to print Hello World or any message?

You can use the following methods –

  • print() method
  • sys.stdout.write() method by importing the sys module
  • Using python-f string
Advertisements