Scrollable Nav Bar

Simple Python program to print “Hello, World!”

The classic print “Hello, World!” program is the first step for many people learning a new programming language. In Python, printing text to the screen is simple and readable — which is one reason Python is such a popular first language. This article explains how to write a Python program that prints "Hello, World!", shows variations, and walks through what the code does and how to run it.


Here’s the “Hello World” program:

Create a file named hello.py and add this single line:

# hello.py
print("Hello, World!")

Output:

When you run this script, Python executes the print() function and displays the string Hello, World! on the standard output (the terminal or console).


What does print() do?

  • print() is a built-in Python function used to send text (or other values) to standard output.
  • The text to be printed must be a string (characters enclosed in single ' or double " quotes) or other objects that Python converts to strings.
  • print() accepts additional optional parameters such as sep, end, file, and flush to control formatting and destination.

Example with optional parameters:

# prints: Hello-World
print("Hello", "World", sep="-", end="\n")  

Running the program

Method 1 — From the command line (Windows/macOS/Linux)

  1. Save the file as hello.py in a folder.
  2. Open a terminal or command prompt and navigate to that folder.
  3. Run:
python hello.py

or, if your system uses python3 for Python 3.x:

python3 hello.py

You should see:

Hello, World!

Method 2 — Using the Python REPL (interactive shell)

Open the Python interpreter by running python or python3 in your terminal, then type:

The REPL is useful for quick experiments and learning.

Output


Variations and alternatives

Single vs double quotes

Both single and double quotes work the same way:

print('Hello, World!')
print("Hello, World!")

Use whichever helps readability — double quotes are common when the string contains an apostrophe.

Using sys.stdout.write()

For lower-level control you can write directly to sys.stdout:

import sys
sys.stdout.write("Hello, World!\n")

This method does not automatically append a newline unless you include \n.

f-strings and variables

Printing variables or formatted text is easy with f-strings (Python 3.6+):

name = "World"
print(f"Hello, {name}!")

Common mistakes to avoid

  • Missing parentheses: In Python 3, print is a function and requires parentheses. print "Hello" is invalid in Python 3 (it was valid in Python 2).
  • Incorrect quotes: Mismatching quotes like print("Hello') will raise a SyntaxError.
  • Forgetting the newline: If using sys.stdout.write() forget to add \n if you expect a new line.
  • Running with the wrong Python version: On some systems python may map to Python 2.x. Use python3 if needed.

Complete Code:

#!/usr/bin/env python3
"""A tiny Python script that prints Hello, World!"""

def main():
    # print a greeting to the console
    print("Hello, World!")

if __name__ == "__main__":
    main()

This structure (a main() function plus the if __name__ == "__main__" guard) is a good habit for scripts that may later grow into larger programs or be imported as modules.