Know some Python? Write your own interactive tutorial, with runnable examples and exercises, and share it with a link. Write and share your own tutorial.

Start writing

Learn Python / Hello, World!

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!")
Output

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")
Output

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")
Output

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')
Output

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")
Output

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)
Output

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!")
Output

What happened:

  • sep="-" put a - between the values instead of a space, so you get 2026-10-05.
  • end="..." ended the line with ... instead of a new line. So the next print carried on in the same line: Loading...done!.

Use sep="" to put nothing at all between the values:

print("Py", "thon", sep="")
Output

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")
Output

\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\"")
Output

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")
Output

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")
Output

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:

  1. The last line names the problem: a NameError, because Python doesn't know anything called pritn.
  2. 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
Output

Exercise 2: A three-line banner

Using a single print() call, print this banner.

**********
 Welcome!
**********
# Print the banner with one print() call
Output

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!"
Output

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. sep changes what goes between them (a space by default) and end what goes after them (a new line by default).
  • \n starts a new line inside a string, and \t adds 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.

Share this tutorial

Know something worth teaching? Write your own tutorial with runnable examples and share it.