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
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, so it works with nothing to set up first.
  • 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.
Prefer to watch? This tutorial is also a video (4:04). The written steps continue below.

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. Python runs the code on the page, and the output panel shows exactly what it 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?

print is the function, the parentheses call it, and the quotes mark the text it prints.

  • print is the name of a function, a named piece of code you can run. Python ships with it, so there is nothing to 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 variables 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?

Press Run on an example, or install Python and run a .py file from a terminal or an editor.

  1. On the lesson page. Press an example's Run button, with nothing to install. The first click downloads Python 3.14, about 6 MB, in a form that runs inside a web page, and your browser keeps it for later visits.
  2. On your computer. Install Python (the Installing Python lesson covers each system and which version to get) and 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. The last line of the message names the problem, so read that line first.

  • 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. The lesson editor runs your code as a file named main.py, which is why that name appears in the message.

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 Hello World examples run on the lesson page with nothing to install. To run the program on your own computer, install Python, save it as hello.py 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 in 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.