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:
Hello, World!
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 assep,end,file, andflushto 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)
- Save the file as
hello.pyin a folder. - Open a terminal or command prompt and navigate to that folder.
- Run:
python hello.pyor, if your system uses python3 for Python 3.x:
python3 hello.pyYou 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:
>>> print("Hello, World!")
Hello, World!Code language: PHP (php)
The REPL is useful for quick experiments and learning.
Output
Hello, World!
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,
printis 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 aSyntaxError. - Forgetting the newline: If using
sys.stdout.write()forget to add\nif you expect a new line. - Running with the wrong Python version: On some systems
pythonmay map to Python 2.x. Usepython3if 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.
