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. print() is a built-in command that displays whatever you put between its parentheses. Python calls commands like this functions, and you'll meet many more.

A program can have many lines. Python runs them one at a time, from top to bottom.

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.

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. print() can show numbers and the results of calculations, not just text.

Changing the separator and the line ending

print() has two handy options:

  • sep sets what goes between values (a space by default).
  • end sets what goes after the last value (a new line by 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

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

Comments

Anything after a # on a line is a comment. Python ignores comments. They're notes for the humans reading your code.

# This line is a comment, so Python skips it
print("Comments are ignored")  # A comment can also 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.

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 a NameError, because it doesn't know any function called pritn. Read error messages from the bottom up: the last line names the problem, and the line above it points to where it happened. 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

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.
  • Strings are text inside matching "double" or 'single' quotes.
  • Separate several values with commas. Use sep and end to control the spacing.
  • # starts a comment, which Python ignores.
  • Error messages tell you what went wrong and on which line. Read them from the bottom up.