Hello, World!
Every programming journey starts with the same tiny program: one that prints Hello, World! to the screen. In Python, it takes just one line.
Press Run below to try it. Python runs right in your browser, so there's nothing to install. The first run takes a few seconds while Python loads.
print("Hello, World!")That's a complete Python program. Here is what each part does:
| Part | What it is |
|---|---|
print |
The name of a built-in command that shows something on the screen. |
( ) |
Parentheses hold what you're giving to the command. They always come in pairs. |
"Hello, World!" |
The value to show. The quotes mark it as text; they aren't printed. |
Python calls commands like print() functions, and you'll meet many more. Running a function is called calling it.
A program can have many lines. Python runs them one at a time, from top to bottom:
print("First")
print("Second")
print("Third")Tip: You can edit any code on this page. Change the message, then press Run again (or press Ctrl+Enter inside the editor).
Strings
The text inside the quotes is called a string. Python accepts either double quotes or single quotes, as long as they match:
print("I am a string in double quotes")
print('I am a string in single quotes')
print("It's easy to use an apostrophe inside double quotes")Each print() call writes its output on a new line.
The quotes must match. If you start with ", end with ". To show an apostrophe, wrap the string in double quotes (as above). To show double quotes, wrap it in single quotes:
print('She said "hi" to me')Quotes also decide what Python does with a value. Without quotes, Python treats 2 + 3 as maths. With quotes, "2 + 3" is just text and is printed exactly as written:
print(2 + 3)
print("2 + 3")Printing several things at once
You can pass more than one value to print() by separating them with commas. Python puts a space between them for you:
print("Python", "is", "fun")
print("2 + 3 =", 2 + 3)Notice that 2 + 3 was calculated before printing, but the text "2 + 3 =" was printed as it is. The commas aren't printed; they only separate the values.
Changing the separator and the line ending
print() has two options you can set by writing name=value after the values:
| Option | What it controls | Default |
|---|---|---|
sep |
What goes between the values | a space, " " |
end |
What goes after the last value | a new line |
You only add them when you want something different from the default.
Predict first: before you press Run, guess what the three lines will print.
print("2026", "10", "05", sep="-")
print("Loading", end="...")
print("done!")What happened:
sep="-"put a-between the values instead of a space, so you get2026-10-05.end="..."ended the line with...instead of a new line. So the nextprintcarried on in the same line:Loading...done!.
Use sep="" to put nothing at all between the values:
print("Py", "thon", sep="")Starting a new line inside a string
Writing \n inside a string starts a new line. The backslash tells Python "the next character is special", so \n means "new line" rather than the letter n:
print("Line one\nLine two\nLine three")\n is called an escape sequence. You'll mostly use these:
| Write | To get |
|---|---|
\n |
a new line |
\t |
a tab (a wide gap, handy for lining things up) |
\" or \' |
a quote that doesn't end the string |
\\ |
one backslash |
print("Name:\tAda")
print("She said \"hi\"")Comments
Anything after a # on a line is a comment. Python ignores comments. They're notes for the humans reading your code.
# Python skips this line
print("Hi") # comments can follow code
# print("This line will not run")Putting # in front of a line is a quick way to switch it off without deleting it. Use comments to explain why the code does something, not to repeat what it obviously does.
Your first error
Mistakes are a normal part of programming, and Python tells you exactly what went wrong. Run this code:
print("Hello")
pritn("World")Python printed Hello and then stopped with an error message like this:
Traceback (most recent call last):
File "main.py", line 2, in <module>
pritn("World")
NameError: name 'pritn' is not defined
Read it from the bottom up:
- The last line names the problem: a
NameError, because Python doesn't know anything calledpritn. - The lines above show where:
line 2, and the code on that line.
Line 1 had already run, which is why Hello was printed. Python stops at the first error, so nothing after it runs. Fix the typo and run it again.
Other common beginner errors:
| Mistake | Example | Error |
|---|---|---|
| Missing quote | print("Hello) |
SyntaxError |
| Missing parenthesis | print("Hello" |
SyntaxError |
| Capital letter | Print("Hello") |
NameError |
| Text without quotes | print(Hello) |
NameError |
A SyntaxError means Python couldn't understand the code at all, so nothing runs, not even the lines before the mistake. A NameError happens while the program runs, when Python reaches a name it doesn't know.
Python is case-sensitive: print and Print are different names.
Exercises
Time to practise. Write your code in main.py and press Run.
- When your output is right, you'll see a green Correct! message under the exercise.
- Stuck? Press Hint for a nudge in the right direction. Some exercises have more than one hint.
- Still stuck? Show answer opens one possible solution in
answer.py. Try the hints first: you learn the most by working it out yourself.
Exercise 1: Say your name
Print a greeting that includes your name, such as Hello, my name is Ada!.
# Print a greeting with your name
Use print() with your greeting inside quotes.
Text must be in quotes, like print("Hello, my name is ...").
Exercise 2: A three-line banner
Using a single print() call, print this banner.
**********
Welcome!
**********
# Print the banner with one print() call
\n inside a string starts a new line, so a single string can hold all three lines.
Put \n between the three parts, and keep the space before Welcome!.
Exercise 3: Fix the bugs
This program has three bugs. Fix them so it prints three lines without any errors.
Print("Python is great")
print('I love coding")
print("Let's go!"There is one bug on each line. Run the code and read the error: it tells you which line is wrong.
Line 1: Print must be lowercase. Line 2: the quotes don't match. Line 3: a bracket is missing.
Summary
print()displays values on the screen, and Python runs your lines from top to bottom.- Strings are text inside matching
"double"or'single'quotes. Without quotes, Python treats things as code or maths. - Separate several values with commas.
sepchanges what goes between them (a space by default) andendwhat goes after them (a new line by default). \nstarts a new line inside a string, and\tadds a tab.#starts a comment, which Python ignores.- Error messages tell you what went wrong and on which line. Read them from the bottom up.
Know something worth teaching? Write your own tutorial with runnable examples and share it.