Your First Python Program: Hello World Explained
Writing a Hello World program is a common first step when learning Python. It helps you confirm that Python is installed, shows you how to run code, and introduces the idea that a program is just a set of instructions for the computer.
If you are completely new to Python, this is the right place to start.
Quick start
print("Hello, world!")
Save this in a .py file or run it in the Python interpreter to print text to the screen.
What Hello World means
Hello World is the traditional first program in many programming languages.
It is useful because it helps you:
- check that Python is installed and working
- run a very small program successfully
- understand that code tells the computer what to do
The program is simple on purpose. You do not need to learn many rules before you can make Python do something visible.
The first program
Here is the full program:
print("Hello, world!")
When you run this line, Python displays this output:
Hello, world!
What this line does
print()is a Python functionprint()displays output in the terminal or console"Hello, world!"is text- Text inside quotes is called a string
Python reads the line from left to right and runs the print() function with the text you gave it.
If you want a deeper explanation of this function, see Python print() function explained.
How to run the program
A simple way to run your first Python program is to save it in a file.
1. Create a file
Create a new file named:
hello.py
2. Add the code
Put this code in the file:
print("Hello, world!")
3. Save the file
Make sure the file ends with .py, not .txt.
Good:
hello.py
Wrong:
hello.py.txt
4. Run the file
Open a terminal or command prompt in the folder where the file is saved, then run one of these commands:
python hello.py
or:
python3 hello.py
5. Check the output
You should see:
Hello, world!
If you want more help with terminals, editors, or IDEs, read how to run Python code from the command line and IDEs.
Breaking the line into parts
Let’s look at the program one piece at a time:
print("Hello, world!")
print
print is a built-in Python function.
A function is a tool that does something when you use it. In this case, it shows output on the screen.
()
The parentheses call the function.
They tell Python: “run this function now.”
"Hello, world!"
This is the value being passed into print().
Because it is inside quotation marks, Python treats it as text.
That text is a string. If you are new to strings, see Python strings explained.
Why the quotes matter
Quotes are required here because the message is text.
This works:
print("Hello, world!")
This does not:
print(Hello, world!)
Without quotes, Python tries to read Hello and world as names instead of text. That often leads to an error such as NameError: name is not defined or a SyntaxError: invalid syntax.
Common beginner mistakes
These are some very common problems when writing your first Python program.
Forgetting quotation marks
Wrong:
print(Hello, world!)
Correct:
print("Hello, world!")
Without quotes, Python does not know that this is text.
Using smart quotes
Sometimes code copied from a website, document, or phone uses curly quotes like these:
print(“Hello, world!”)
That will usually cause an error.
Use normal straight quotes instead:
print("Hello, world!")
Misspelling print
Python is case-sensitive.
These are wrong:
Print("Hello, world!")
pint("Hello, world!")
This is correct:
print("Hello, world!")
Saving the file with the wrong extension
If your file is saved as hello.py.txt, Python may not run it as a Python file.
Make sure the file is really named:
hello.py
Running the command in the wrong folder
If your terminal is not in the same folder as hello.py, Python may say it cannot find the file.
Helpful commands:
python --version
python3 --version
python hello.py
python3 hello.py
pwd
ls
dir
These commands can help you:
- check whether Python is installed
- confirm which folder you are in
- list the files in the current folder
Simple variations to try
Once Hello, world! works, try small changes.
Print a different message
print("My name is Sam.")
Print two lines
print("Hello, world!")
print("I am learning Python.")
Output:
Hello, world!
I am learning Python.
Try another string
print("Python is working.")
For now, keep your focus on basic output. You do not need user input yet.
What to learn next
After your first program works, the next useful topics are:
- basic Python rules and structure in Python syntax basics explained
- strings, because
"Hello, world!"is a string - the
print()function for printing multiple values and formatting - different ways to run Python code in files, terminals, and editors
FAQ
Why is Hello World used as the first program?
It is simple, easy to test, and helps confirm that your Python setup works.
Do I need to save the code in a file first?
No. You can also type it directly into the Python interpreter.
However, saving it in a file is useful because it teaches you how Python scripts work.
What does print() do in Python?
print() shows output on the screen.
Why are quotation marks needed?
Quotation marks tell Python that Hello, world! is text, not a variable or function name.
Why do I get a NameError when I type Hello, world! without quotes?
Without quotes, Python tries to treat the words as names instead of text.
See also
- How to run Python code from the command line and IDEs
- Python syntax basics explained
- Python strings explained: basics and examples
- Python
print()function explained - NameError: name is not defined
- SyntaxError: invalid syntax
If you can run this program successfully, you have already completed an important first step. Next, continue with syntax basics and learn more about print() so you can write slightly larger Python programs with confidence.