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.
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).
print() do?print() is a built-in Python function used to send text (or other values) to standard output.' 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") hello.py in a folder.python hello.pyor, if your system uses python3 for Python 3.x:
python3 hello.pyYou should see:
Hello, World!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!
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.
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.
Printing variables or formatted text is easy with f-strings (Python 3.6+):
name = "World"
print(f"Hello, {name}!")
print is a function and requires parentheses. print "Hello" is invalid in Python 3 (it was valid in Python 2).print("Hello') will raise a SyntaxError.sys.stdout.write() forget to add \n if you expect a new line.python may map to Python 2.x. Use python3 if needed.#!/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.