The hello world program in Python is one line: print("Hello, World!"). Save it as hello.py and run python hello.py (or py hello.py on Windows), and the terminal prints Hello, World!.
This page assumes Python 3 is already installed. If the command is missing, start with how to install Python on Windows. Everything below works the same on Windows, macOS and Linux except the command that starts Python, which the table in the next section covers.
Write and run hello.py
Create a new file called hello.py in any folder. A plain text editor is enough. Put this single line in it:
print("Hello, World!")
Open a terminal in that folder and run the file:
python hello.py
Example output:
Hello, World!
That is the whole program. print is a built-in function, the text between the quotes is a string, and the terminal shows whatever the function writes. The command that starts Python differs by system:
| System | Command to try | Notes |
|---|---|---|
| Windows | py hello.py | py is the Python launcher. python hello.py also works when the python command is set up. |
| macOS | python3 hello.py | Many Macs have no plain python command, so use python3. |
| Linux | python3 hello.py | Same as macOS on most distributions. |
The official tutorial describes this form: when the interpreter is called with a file name argument, it reads and executes a script from that file. Its own examples use a versioned command such as python3.15, which is why python3 is the safe habit on Unix-like systems. On Windows the tutorial notes that if the py.exe launcher is installed, you can use the py command.
If the python command on Windows opens the Microsoft Store instead of running your file, see app execution aliases and Python on Windows.
Try it without a file: the interactive interpreter
Run python (or python3, or py) with no file name and Python starts in interactive mode. It waits for commands at the primary prompt, which is usually three greater-than signs:
>>> print("Hello, World!")
Hello, World!
>>>
Each line runs as soon as you press Enter. This is the quickest way to test a single statement. To leave, type quit(), or press Ctrl+Z then Enter on Windows, or Ctrl+D on Unix. The Python docs say an end-of-file character at the primary prompt makes the interpreter exit with a zero exit status.
Interactive mode shows a second prompt, ..., for continuation lines. You see it when you start a block such as an if statement and Python is waiting for the rest of it.
What each part of the line does
The line print("Hello, World!") has three pieces:
printis the name of a built-in function. Python finds it without any import.( )calls the function. The values inside the parentheses are its arguments. In Python 3 the parentheses are not optional."Hello, World!"is a string literal. Single quotes work the same way:'Hello, World!'.
The documented signature is print(*objects, sep=' ', end='\n', file=None, flush=False). Each object you pass is converted to a string the same way str() does, objects are separated by sep, and end is written after the last one. The default end is a newline, which is why the next prompt appears on its own line. The sep, end, file and flush arguments must be given by keyword.
A few variations to try
Change the file and run it again each time. Printing more than one value puts a space between them by default:
print("Hello,", "World!")
Example output:
Hello, World!
Use end to change what comes after the text. An empty string removes the newline, so two calls print on one line:
print("Hello, ", end="")
print("World!")
Example output:
Hello, World!
Store the text in a variable and print that:
greeting = "Hello, World!"
print(greeting)
Greet someone by name using an f-string, which puts the value of a variable inside the text:
name = "Ada"
print(f"Hello, {name}!")
Example output:
Hello, Ada!
For more on these options, see print() function examples and Python f-strings.
Hello World as a function
Once the one-liner works, a slightly longer version shows the structure most real scripts use. It defines a function and calls it only when the file is run directly:
def main():
print("Hello, World!")
if __name__ == "__main__":
main()
The body of main is indented. Python uses indentation, not braces, to mark a block, and the indentation must be consistent. The check __name__ == "__main__" is true when you run the file as a script and false when another file imports it, so importing the file does not print anything. For a one-line program this is more than you need, but it is a good habit once a file has real code in it.
Common errors and how to fix them
Most failures on a first run come from a handful of causes.
SyntaxError on a print statement
Older tutorials show print "Hello, World!" without parentheses. That is Python 2 syntax and Python 3 rejects it. Recent Python 3 versions report a SyntaxError and suggest the fix. Add the parentheses: print("Hello, World!").
NameError from a capital letter
Python is case sensitive. Print("Hello, World!") fails with a NameError because the function is called print, all lowercase. Recent versions add a hint such as Did you mean: 'print'?.
Mismatched quotes
A string must end with the same kind of quote it started with. print("Hello, World!') is a SyntaxError because the string never closes. Check both ends of the text and the closing parenthesis.
Unexpected indent
A line that starts with spaces in the wrong place raises an IndentationError. In the one-line program, make sure print starts in the first column. The cause and fixes are covered in IndentationError: unexpected indent.
The file cannot be found
If Python reports that it cannot open the file, your terminal is in a different folder from hello.py. Change into the folder that holds the file, or give the full path to it. Also check that the editor did not save it as hello.py.txt. Windows hides known file extensions by default, so a wrongly named file can look correct in Explorer.
Non-English text prints wrongly
By default, Python treats source files as UTF-8, so saving the file as UTF-8 is the safe choice. If the characters still look wrong in the terminal, the cause is usually the terminal's own font or code page, not the program.
Where to run Python code
A terminal and a text editor are enough, but you have other options. A code editor such as Visual Studio Code can run the open file with one button, and it highlights Python syntax as you type (see Python syntax highlighting in VS Code). IDLE, the editor that ships with Python on some systems, also has a run command. Whichever you pick, the program and its output are the same.
What to learn next
With Hello World running, a sensible order is: variables and strings, reading input with input(), if statements, loops, then functions. Each of those can be tried one line at a time in the interactive interpreter before you put it in a file. When you start installing libraries, create a virtual environment for each project so their packages do not mix, as described in Python virtual environments with venv.
Frequently asked questions
- What is the simplest Hello World program in Python?
- It is one line: print("Hello, World!"). Save it in a file ending in .py and run that file with the Python interpreter, or type it at the >>> prompt of the interactive interpreter. In Python 3, print is a function, so the parentheses are required.
- Why does print "Hello, World!" give a SyntaxError?
- That is Python 2 syntax. In Python 3, print is a function and needs parentheses, so write print("Hello, World!"). Recent Python 3 versions report this as a SyntaxError that suggests adding the parentheses.
- Do I need to save the file to run Hello World?
- No. You can type print("Hello, World!") at the >>> prompt of the interactive interpreter and it runs straight away. Saving a file is the way to keep the program and run it again later.
- What is the difference between python and py on Windows?
- py is the Python launcher for Windows. It finds an installed Python for you, so it works even when the python command is not set up. Both can run a script: py hello.py or python hello.py.