Python Hello World Example Explained
This beginner-friendly example shows the simplest useful Python program. You will learn what it does, how to run it, and how to fix a few common mistakes.
print("Hello, world!")
Save this in a .py file or run it in the Python interpreter to see your first working Python output.
What this example shows
This example teaches a few important basics:
- The smallest useful Python program
- How to display text on the screen
- How Python runs code from top to bottom
- Why this is often the first program beginners write
Even though it is only one line long, it introduces an important idea: Python can execute your code and produce visible output right away.
The Hello World code
Here is the full program:
print("Hello, world!")
What this line means:
print()sends output to the screen"Hello, world!"is the text to display- The text is inside quotes, so Python treats it as a string
- In Python 3, the parentheses are required
If you want a deeper explanation of this function, see Python print() function explained.
Expected output
When you run the code, the output should be:
Hello, world!
A beginner mistake is to confuse code with output.
- Code is what you write:
print("Hello, world!") - Output is what Python shows after running the code:
Hello, world!
You will usually see the output in one of these places:
- The terminal
- The Python shell
- The output panel in an IDE
How to run the example
Before running the program, make sure Python is installed on your computer.
Run it in the Python interpreter
Open a terminal or command prompt and start Python:
python
On some systems, you may need:
python3
Then type:
print("Hello, world!")
Press Enter, and Python should display:
Hello, world!
Run it from a file
Create a file named hello.py and put this code inside it:
print("Hello, world!")
Then run the file from your terminal:
python hello.py
Or, on some systems:
python3 hello.py
Run it in an IDE
You can also run the program in tools like:
- VS Code
- IDLE
Create a new Python file, paste in the code, and use the IDE's run button or menu option.
If you need more help with this step, see how to run Python code from the command line and IDEs.
Line-by-line explanation
This program has only one line:
print("Hello, world!")
Here is what each part does:
printis a built-in Python function- The parentheses
()hold the value passed to the function - The quotes tell Python that
Hello, world!is text, not code - After printing the message, the program ends
You can think of it like this:
- Function name:
print - Value given to the function:
"Hello, world!"
If you are new to Python syntax, Python syntax basics explained is a good next step.
Simple variations to try
Once the first version works, try changing it.
Change the message text
print("Welcome to Python!")
Print your name
print("My name is Sam")
Use two print() lines
print("Hello, world!")
print("I am learning Python.")
Expected output:
Hello, world!
I am learning Python.
Python runs code from top to bottom, so the first line prints before the second line.
Store text in a variable and print the variable
message = "Hello, world!"
print(message)
This introduces a variable, which is a name that stores a value.
Common beginner mistakes
Here are some problems beginners often run into.
Forgetting quotation marks
This code is wrong:
print(Hello, world!)
Python will not understand Hello, world! as text because it is not inside quotes.
Use this instead:
print("Hello, world!")
If you forget quotes, you may get an error such as NameError: name is not defined.
Misspelling print
This is wrong:
pritn("Hello, world!")
Function names must be spelled exactly right.
Correct version:
print("Hello, world!")
Using smart quotes
Sometimes code copied from a document or messaging app uses curly quotes like these:
print(“Hello, world!”)
Those are not normal Python string quotes. Use straight quotes instead:
print("Hello, world!")
Running Python code in the wrong tool
Make sure you are actually running Python.
Useful commands to check:
python --version
python hello.py
python3 --version
python3 hello.py
Common causes of problems include:
- Python is not installed or not added to PATH
- The file is saved with the wrong extension
- Quotes are missing around
Hello, world! - The code uses curly quotes instead of normal quotes
- The program is run with the wrong command
If Python reports a syntax problem, see SyntaxError: invalid syntax.
FAQ
Why does Hello World use print()?
Because print() is the simplest way to show output on the screen in Python.
Do I need to save the code in a file first?
No. You can run it directly in the Python interpreter or save it in a file and run that file.
Why am I getting a syntax error?
This usually happens if the quotes, parentheses, or spelling are incorrect.
What should I learn after Hello World?
A good next step is Python syntax, variables, strings, and how to run Python code.