Learn

Short lessons with real code you can edit and run on the page.

Hello World in Python

Lesson 1 Python 3.14 Runs in your browser Updated
In short

print("Hello, World!") is the one-line program that prints the words Hello, World! to the screen. It is the first thing most people write in a new language, because it proves everything works and shows you how output looks.

Key facts

  • print() is a built-in function. It works without importing anything.
  • The text between the quotes is a string. The quotes themselves are not printed.
  • Every print() call ends with a new line unless you tell it otherwise.
  • Python is case sensitive. Print is not print.

How do you write Hello World in Python?

Type print("Hello, World!") and run it. Python calls the built-in print() function with one argument, the text inside the quotes, and shows that text on the screen followed by a new line.

Python
print("Hello, World!")
Output
Hello, World!

Press Run. The code executes in your browser, and the output panel shows exactly what Python printed. Change the text between the quotes and run it again. Nothing you type here leaves your computer.

What does each part of print("Hello, World!") do?

Three pieces do all the work, and each has one job.

  • print is the name of a function, a named piece of code you can run. Python ships with it, so there is nothing to import or set up.
  • The parentheses call the function, which means they run it. Whatever sits between them is handed to print() as an argument.
  • The quotes mark a string, which is Python's word for text. Double or single quotes both work, as long as the pair matches.
Python
print('Hello, World!')
Output
Hello, World!

Leave the quotes off a single word, as in print(Hello), and Python looks for a variable named Hello (a name for a stored value, covered in the next lesson), finds none, and stops with NameError: name 'Hello' is not defined.

How do I print more than one line?

Call print() once per line. Each call ends with a new line, so the outputs stack up in order.

Python
print("Hello, World!")
print("Hello, Python!")
print("Three lines, three calls.")
Output
Hello, World!
Hello, Python!
Three lines, three calls.

print() also accepts more than one argument. It prints them in order with a single space between them.

Python
print("Hello,", "World!")
Output
Hello, World!

Numbers don't need quotes. Without quotes Python does the math first and prints the result. With quotes it prints the characters exactly as you typed them.

Python
print(2 + 2)
print("2 + 2")
Output
4
2 + 2

How do I run a Python program?

You have three options, and the first one needs nothing installed.

  1. In this lesson. Every example has a Run button. The first click downloads Python 3.14 as WebAssembly (about 6 MB, cached after that), and from then on your code runs inside the page.
  2. On your computer. Install Python (the Installing Python lesson covers each system), save print("Hello, World!") in a file called hello.py, open a terminal in the folder that holds it and run python hello.py, or python3 hello.py on macOS and Linux.
  3. In an editor. Once Python is installed, Visual Studio Code with the Python extension shows a run button at the top right of any .py file. The output appears in the integrated terminal.

Common mistakes with Hello World in Python

Each of these four Hello World mistakes produces an error message you'll see again. Read the last line first. It names the problem.

  • NameError: name 'Hello' is not defined. The quotes are missing, so Python reads Hello as a variable name. Wrap the text in quotes.
  • SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)? The parentheses are missing, as in print "Hello", the Python 2 form. Python 3 needs them.
  • SyntaxError: unterminated string literal (detected at line 1). A closing quote is missing or the pair doesn't match, like "Hello'. Use the same quote on both ends.
  • NameError: name 'Print' is not defined. Did you mean: 'print'? The P is capitalized. Python is case sensitive, and the function is print in lowercase.

Run this broken line to see the first one for yourself, then add the quotes and run it again.

Python
print(Hello)
Output
Traceback (most recent call last):
  File "main.py", line 1, in <module>
    print(Hello)
          ^^^^^
NameError: name 'Hello' is not defined

Exercise

Change the program so it prints Hello, World! exactly, with the comma and the exclamation mark.

Python
print("Goodbye, World!")
Output
Show the solution
print("Hello, World!")

Quiz

This quiz has 4 questions. Pick an answer to see why it is right or wrong.

  1. 1What does this program print?

    print("2 + 2")
  2. 2Which line prints Hello, World! in Python 3?

  3. 3What does this program print?

    print("Hello,", "World!")
  4. 4What happens when you run Print("Hi")?

Frequently asked questions

How do you write Hello World in Python?
Write print("Hello, World!") on its own line, save it in a file such as hello.py and run it. Python prints the text and moves to a new line.
Do I need to install Python to run Hello World?
No. The examples on this page run in your browser. To run it on your own computer, install Python and run python hello.py in a terminal, or python3 hello.py on macOS and Linux.
Why is it called Hello World?
Because the program prints exactly those words. Its first known use in a program is Brian Kernighan's 1972 tutorial for the B language at Bell Labs, and the 1978 book The C Programming Language made it the standard first program.
Can I print Hello World without parentheses?
No. In Python 3 print is a function, so the parentheses are required. The form print "Hello" belonged to Python 2, which stopped being supported in January 2020.
Does print add a new line?
Yes. print() ends its output with a newline character by default. Pass end="" as an extra argument to keep the next output on the same line.
How do I print Hello World in Visual Studio Code?
Install Python and save print("Hello, World!") in a file ending in .py. Then add the Python extension and press the run button at the top right of the editor. The output appears in the integrated terminal.