By tradition, the first program you write in a new language prints the message Hello, World! to the screen. It’s a small program, but it proves that everything works: Python is installed, your editor is set up, and you know how to run code.
In Python, the whole program is a single line. In this tutorial, you’ll write and run it, understand exactly what each part of that line means, and then extend it step by step with comments, variables, user input, and formatted output. You’ll also learn how to read and fix the errors beginners run into most often.
Key terms used in this article:
- Terminal: A text window where you type commands, such as Command Prompt or PowerShell on Windows and Terminal on macOS and Linux.
- Function: A named piece of code that does a job, such as
print(), which displays text. - String: A piece of text in quotation marks, such as
"Hello, World!". - Variable: A name that stores a value so you can use it later.
- Error message: Python’s explanation of what went wrong and on which line.
Table of contents
- Before you begin
- Write your first Python program
- Hello World explained
- Python vs other languages
- More ways to use print()
- Add comments to your program
- Use variables and f-strings
- Make it interactive with input()
- Optional: Structure a program with a main() function
- Common beginner errors and how to fix them
- Practice exercises
- Summary
Before you begin
You need two things:
- Python 3 installed. Open a terminal and run
python --version(Windows) orpython3 --version(macOS and Linux). If you see a version number, you’re ready. If not, follow How to Install Python on Windows, macOS, and Linux. - A code editor. Any editor works, including VS Code, PyCharm, or IDLE, which comes with Python. See Setting Up VS Code, PyCharm, and Jupyter for Python.
Note: In this tutorial, terminal commands use python. On macOS and Linux, type python3 instead, unless you’re inside an activated virtual environment.
Write your first Python program
Step 1: Create a file
Create a folder for your Python practice, for example python_projects. Inside it, create a new file named hello.py and type this single line:
Save the file. A few rules to follow:
- The file name must end with
.py. That tells your computer and editor it’s a Python file. - Use lowercase letters, numbers, and underscores in file names, such as
hello_world.py. Avoid spaces. - Don’t name your file after a Python module, such as
random.pyormath.py. It can stop that module from being imported later. - Type the code exactly, including the lowercase
print, the parentheses, and the quotation marks. Python is case-sensitive.
Step 2: Run the program
Open a terminal, move into the folder that contains your file with the cd command, and run the file with Python:
Output:
Hello, World!
Congratulations, you’ve written and run your first Python program! The command python hello.py tells the Python interpreter to read hello.py and run every line in it from top to bottom.
Other ways to run it
1. In the interactive shell
Type python on its own in the terminal and press Enter. You’ll see the >>> prompt. Python runs each line as soon as you press Enter, which is perfect for quick experiments:
Code typed in the shell isn’t saved. Use a .py file for anything you want to keep.
2. From your editor
- VS Code: Click the Run Python File button (▷) in the top-right corner.
- PyCharm: Click the green Run icon (▶) or right-click and choose Run ‘hello’.
- IDLE: Open the file and press F5, or choose Run › Run Module.
Hello World explained
Let’s break down every part of print("Hello, World!").
| Part | Name | What it does |
|---|---|---|
print | Built-in function | A ready-made function that displays values on the screen (the “standard output”). |
( ) | Parentheses | Call the function. Whatever is inside them is passed to the function as an argument. |
"Hello, World!" | String literal | A piece of text. The quotation marks show where the text starts and ends. They aren’t printed. |
So the line means: “call the print function and give it the text Hello, World! to display”.
What is a function?
A function is a named block of code that performs a task. Python comes with many built-in functions you can use without importing anything, such as print(), input(), len(), and type(). To use a function, you write its name followed by parentheses. This is called calling the function.
What is a string?
A string (type str) is a sequence of characters: letters, digits, spaces, and symbols. You can write strings in single quotes or double quotes. Both work the same way:
What is a statement?
Each line of instructions is a statement. In Python, a statement usually ends at the end of the line, so you don’t need a semicolon (;). You also don’t need to wrap the code in a class or a main() function, as you do in Java or C. Python simply runs the file from top to bottom.
If you’re curious about what Python does behind the scenes when you run a file, read How Python Works: Interpreter, Bytecode, and CPython Explained. You don’t need it to keep learning, though.
Python vs other languages
Comparing Hello World across languages shows why Python is considered beginner-friendly. Here is the same program in Java:
And in C:
Both need extra structure, such as a class, a main function, braces, semicolons, and in C, an #include line and a separate compile step. In Python, you write only the line that does the work.
More ways to use print()
print() is the function you’ll use most while learning, so it’s worth knowing its main options.
Print several values
Pass several values separated by commas. print() puts a space between them automatically. You can also join strings with + and repeat them with *:
Notice that print() can display numbers too, not just strings.
Change the separator and line ending
sepsets what goes between values (a space by default).endsets what goes at the end (a newline by default). Change it to keep the nextprint()on the same line.
Output:
Hello-World Hello World
Quotes and special characters
To include a quotation mark inside a string, use the other type of quote around the string, or put a backslash (\) before it. A backslash also starts special escape sequences, such as \n for a new line and \t for a tab.
Output:
It's a great day He said "Hi" She said, "Hello!" Name: Jessa C:\Users\Jessa
Print several lines
Use \n inside a string, call print() with no arguments to print an empty line, or use a triple-quoted string, which keeps the line breaks exactly as you type them:
Output:
Line 1 Line 2 Hello, World!
Here’s a slightly bigger program that combines these ideas to draw a box around the message:
Output:
******************** * Hello, World! * ********************
Add comments to your program
A comment is a note for people reading your code. Python ignores everything after a # on a line. Use comments to explain why code does something.
The last line is “commented out”, so Python skips it. Commenting out a line is a quick way to disable code temporarily. Learn more in Python Comments.
Use variables and f-strings
A variable is a name that stores a value so you can use it later. Create one with the = sign. To insert variables into a message, use an f-string: put f before the opening quote and wrap each variable in curly braces { }.
Output:
Hello, Jessa My name is Jessa. I am learning Python in 2026.
f-strings are the clearest way to build messages from values, and you’ll use them constantly.
Make it interactive with input()
The built-in input() function shows a prompt, waits for the user to type something and press Enter, and returns what they typed as a string. Let’s make Hello World greet the user by name:
Output:
Enter your name: Jessa Hello, Jessa! Welcome to Python.
Your program now responds to the person running it. That’s the basic pattern of most programs: take input, process it, and produce output.
Optional: Structure a program with a main() function
Optional: You can skip this section for now. You don’t need this pattern for your first programs, but you’ll see it in many Python files, so it helps to recognize it.
As programs grow, Python developers often put the main code inside a function and call it like this:
Output:
Hello, World!
def main():defines a function namedmain. The indented line below it is the function’s body.__name__is a special variable. Python sets it to"__main__"when you run the file directly.- The
ifcheck meansmain()runs when you run the file, but not when another file imports it. This keeps your code reusable.
Common beginner errors and how to fix them
Errors are a normal part of programming. Python’s error messages tell you the file, the line number, the type of error, and often a suggestion. Always read the last line first. Here are the mistakes beginners make most often with Hello World.
1. Capital letter in print: NameError
Traceback (most recent call last):
File "hello.py", line 1, in <module>
Print("Hello, World!")
^^^^^
NameError: name 'Print' is not defined. Did you mean: 'print'?
Fix: Python is case-sensitive. Use lowercase print.
2. Missing closing quote: SyntaxError
File "hello.py", line 1
print("Hello, World!)
^
SyntaxError: unterminated string literal (detected at line 1)
Fix: Every string needs matching quotes at both ends, and they must be the same type ("..." or '...'). Also check that your editor didn’t insert curly “smart quotes” (“ ”), which Python doesn’t accept.
3. Missing closing parenthesis: SyntaxError
File "hello.py", line 1
print("Hello, World!"
^
SyntaxError: '(' was never closed
Fix: Add the closing ).
4. Python 2 syntax: SyntaxError
File "hello.py", line 1
print "Hello, World!"
^^^^^^^^^^^^^^^^^^^^^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?
Fix: In Python 3, print is a function and needs parentheses. Old tutorials written for Python 2 use this outdated form.
5. Unexpected space at the start of a line: IndentationError
File "hello.py", line 2
print("World")
IndentationError: unexpected indent
Fix: In Python, indentation has meaning. Only indent lines that belong to a block, such as the body of a function, loop, or if statement. Top-level lines start at the very left.
6. Wrong file name or folder: “can’t open file”
python: can't open file '/Users/jessa/python_projects/helo.py': [Errno 2] No such file or directory
Fix: Check the spelling of the file name, and make sure your terminal is in the folder that contains the file. Run dir (Windows) or ls (macOS and Linux) to list the files in the current folder.
7. Running a file from inside the Python shell
If you type python hello.py at the >>> prompt, you’ll get a SyntaxError, because the shell expects Python code, not terminal commands. Type exit() to leave the shell first, then run the command in the terminal.
Practice exercises
Try these small tasks to practice what you’ve learned. Try each one yourself before looking at the solution.
Exercise 1: Print your name, your city, and your favorite food on three separate lines using a single print() call.
Exercise 2: Print the words Python, is, and fun separated by ***, so the output is Python***is***fun.
Exercise 3: Ask the user for their name and favorite programming language, then print a message such as Hi Jessa, Python is a great choice!
For more practice, solve the Python Input and Output Exercise.
Summary
- The Python Hello World program is one line:
print("Hello, World!"). Save it in a file ending in.pyand run it withpython hello.py(python3on macOS and Linux). printis a built-in function. The parentheses call it, and"Hello, World!"is a string argument passed to it.- Python needs no semicolons, no class, and no
main()function. It runs the file from top to bottom. - Use commas to print several values,
sepandendto control spacing, and escape sequences such as\nand\tfor new lines and tabs. - Comments start with
#. Variables store values, and f-strings such asf"Hello, {name}"insert them into text.input()reads text from the user. - The
if __name__ == "__main__":pattern runs code only when the file is run directly. - Read the last line of an error message first. Most beginner errors come from capital letters, missing quotes or parentheses, Python 2 syntax, unexpected indentation, or running the command from the wrong folder.
Also, See:

Leave a Reply