CodesCompiler
🔍
HTMLCSSJavaScriptPythonSQLPHPSEOPostsBooksKids About Contact 🚀 Get Started Free

PYTHON - Hello World Program

Python - Hello World Program

This tutorial will teach you how to write a simpleHello Worldprogram using Python Programming language. This program will make use of Python built-inprint()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

Consider the below code that will print “Hello World” or any message that you will write inside the print() method on the screen.

PYTHON CODE
# 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

PYTHON CODE
Hello World

Different Ways to Write and Execute Hello World Program

Using Python Interpreter Command Prompt Mode

It is very easy to display theHello Worldmessage using the Python interpreter. Launch the Python interpreter from a command terminal of your Windows Operating System and issue theprint statementfrom the Python prompt as follows −

Example

PYTHON CODE
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

PYTHON CODE
$ 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

PYTHON CODE
print ("Hello World")

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

PYTHON CODE
C:\>python hello.py

This will display the following output

PYTHON CODE
Hello World

To run the program from Linux terminal

PYTHON CODE
$ python3 hello.py

This will display the following output

PYTHON CODE
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

PYTHON CODE
#!/usr/bin/python3

print ("Hello World")

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

PYTHON CODE
$ chmod +x hello.py

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

PYTHON CODE
$ ./hello.py

This will display the following output

PYTHON CODE
Hello World

Thus, we can write and run “Hello World” program in Python using the interpreter mode and script mode.

FAQs

1. Why the first program is called Hello World?

It is just a simple program to test thebasic syntaxand compiler/interpreter configuration ofPython 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?

CodesCompiler developed an online environment where you can run your codes. You can use thePython online compilerto 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