Python Hello World: write and run your first program

Write one instruction and see its result

A school wants to display a welcome message. Before building a complete noticeboard, let’s check that we can write a Python instruction, run it and see text appear. A tiny program lets us test that whole path without introducing a large problem.

Hello World is a traditional first program that displays a greeting. It is useful because its result is easy to recognize. The words are not special commands: you can replace them with a school message once the program works. If the difference between code, interpreter and output is unfamiliar, review the introduction to Python.

Change the message; keep the instruction

Python 3 code

print("Hello, World!")

↓ Run the code

Output

Hello, World!
The text inside quotes becomes the greeting. The surrounding print call tells Python to display it.

What you’ll learn

  • Write a complete one-line Python program that displays a greeting.
  • Run the same code in the online compiler, a local file or an interactive Python shell.
  • Explain why quotes and parentheses belong to the code rather than the displayed message.
  • Modify a greeting, predict two-line output and recognize common first-run mistakes.

Start with the complete one-line program

Here is the entire program. It uses Python 3 and requires no imported library, named variable or input from a user.

print("Hello, World!")

Expected output

Hello, World!

print is a built-in function, reusable code supplied by Python for displaying values. The parentheses enclose what we ask it to display. The matching quotes mark a string, a text value. The capitalization, comma and exclamation mark inside that string become part of the greeting.

Python does not display the surrounding quote marks, parentheses or word print. Those tell it how to interpret the instruction. This print call also ends its output with a newline, so a later print normally begins on the next line. You do not need a semicolon at the end of this line.

Try it online before installing anything

Open the Python online compiler. Replace the editor’s example with the complete program below, then use its Run button. Read the output area after execution finishes.

print("Hello, World!")

Expected output

Hello, World!

This program does not ask for input, so no input text is required. If the output is different, compare the editor contents with the displayed line. If an error appears, read it before changing the code. The online compiler runs submitted code through its service; the guided example later in this lesson follows prepared executions.

Run the same program from a file on your computer

If you prefer to work locally, install a supported Python 3 release from python.org. Use a code editor or a plain-text editor, write the program below and save it as main.py. The .py extension identifies a Python source file; check that it was not saved as main.py.txt.

print("Hello, World!")

Open a terminal, a text interface for running commands, in the folder containing the saved file. On many macOS and Linux installations, the command is:

python3 main.py

On Windows installations with the Python launcher available, use:

py main.py

Some setups provide python instead. Check the command’s version with python3 --version, py --version or python --version, and use the command that invokes your supported Python 3 installation. These are terminal commands, not lines to put inside main.py.

Expected output

Hello, World!

Saving the file stores the code; running the command executes it. After editing the message, save the file again before rerunning it. The interpreter tutorial and Windows setup documentation explain platform-specific invocation.

Try a single instruction in the Python shell

The interactive Python shell lets you enter a small instruction and see its result immediately. Start your installed Python 3 interpreter without a filename—for example, python3 in a suitable terminal setup. A prompt such as >>> indicates that the interpreter is ready for Python code.

The transcript below includes the prompt to show where you type. Enter only print("Hello, World!"), then press Enter; do not type the prompt itself.

>>> print("Hello, World!")
Hello, World!

Inside the Python shell, python3 main.py is not a Python instruction: that command belongs in the terminal outside the shell. You can leave the shell with exit(). In a saved file, omit all >>> prompts and include only your program code.

Follow the same greeting through a name

The one-line version is enough to print a greeting. A two-line version gives the text a name first, as in the introduction. Here is the default sample in full:

greeting = "Hello, World!"
print(greeting)

Expected output

Hello, World!

greeting = ... assigns the text to the name greeting. Assignment itself displays nothing. print(greeting) looks up that name and displays its text. The player shows this sequence; predict when the first output will appear before revealing and stepping through it.

Choose another sample to compare the result. The sample changes the quoted text in the prepared program, not input entered during execution. The named version is useful when you want to refer to the text again; the variables lesson develops that idea.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
greeting = 'Hello, World!'
print(greeting)
Live state

Top-level scope

greeting→Object 1
Object 1 · str'Hello, World!'
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
Hello, World!

What do you expect this program to print?

Display two school notices on separate lines

Now give the screen two instructions. Both are complete print calls in this Python 3 program:

print("Welcome to school!")
print("The library opens at 9.")

Expected output

Welcome to school!
The library opens at 9.

The first call displays the welcome message and ends with a newline. The second displays the library notice on the next line. Python runs these calls in the written order. Swapping the two code lines swaps the two output lines; it does not automatically sort notices or infer which one is more important.

A newline in the code separates the instructions. The newline in the output is supplied by print’s default ending. Those are related here, but they have different roles. The print documentation describes that default behavior.

Practice: write a welcome for your class

Your task: Make this complete program display Welcome to Class 7! followed by Please bring your notebook. on the next line. Change the text, keeping two print calls.

print("Hello, World!")
print("The library opens at 9.")

Worked answer

print("Welcome to Class 7!")
print("Please bring your notebook.")

Expected output

Welcome to Class 7!
Please bring your notebook.

Each quoted string supplies one notice. Both calls keep their matching parentheses and quotes, so only the displayed text changes. Type the program yourself, predict its two lines and compare the result. If you use a local file, remember to save before running.

Check the code and where you are running it

Matching straight quote marks are part of valid Python code. print("Hello, World!) has an opening quote but no closing quote, so Python cannot parse it and raises a SyntaxError. Curly quotation marks copied from formatted text can also break the syntax. Use the straight quotes shown in the examples.

Print("Hello, World!") uses an uppercase P. The built-in name is lowercase print; this misspelling raises a NameError. Python does not automatically correct capitalization. Quoting the entire instruction, as in "print(...)", makes it text rather than a call.

If the terminal says it cannot find main.py, check the folder and filename. If the Python command is not found, check installation and the command documented for your setup. If the old greeting still appears, save your changes and check that you are running the intended file. These are different problems, so use the message and observed behavior to choose the next check.

Check what reaches the output

print("Hello, World!")
What does this Python 3 program display?
Choose one answer. You can try again.

Answer and explanation

Hello, World!

The text inside the quotes becomes output; the call’s syntax is not included.

Check the order of two notices

print("Welcome")
print("Library opens")
Which output matches this complete program?
Choose one answer. You can try again.

Answer and explanation

Welcome Library opens

The first print call displays Welcome, then the second displays Library opens on the next line.

Check the shell prompt in a saved program

What belongs in main.py when copying a Python shell example?
Choose one answer. You can try again.

Answer and explanation

Only the Python code, such as print("Hello, World!").

The shell prompt marks where to type; it is not part of the program saved in the file.

You can now write, run and explain a tiny program

A Hello World program checks the whole path from written instruction to visible output. You can use a direct print call or give the greeting a name first; both can display the same text.

  • Write: put text inside matching quotes and pass it to lowercase print with parentheses.
  • Run: submit code online, execute a saved file, or enter an instruction in the Python shell. Keep terminal commands and shell prompts out of saved Python source.
  • Read: print displays string contents and normally finishes with a newline; two calls produce two lines in order.
  • Change: edit the notice, save if necessary, predict the output and check the actual result.

Before moving on, make sure you can display your own classroom welcome and explain which parts of the code appear in the output and which parts tell Python what to do.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Python Comments: explain your code without changing its output →