Print "Hello, World!" in Python
One line is enough for your first Python script. Print Hello, World!, check that Python runs, and learn what quotes and a terminal newline actually do.
1# if this prints, Python is installed2print("Hello, World!")
Sample Output
Hello, World!
The greeting is printed once, without the quote marks from the source.
In this one
- Call the built-in print function
- Recognize a string literal
- Run a saved Python script
How it works
- 1
Ignore the # comment during execution.
- 2
Read the quoted greeting as one string argument.
- 3
Call print to write the greeting followed by a newline.
Edge cases
Mismatched quotes
Open with a double quote and close with a single quote.
Python cannot parse that string and raises SyntaxError before running the file.
Missing interpreter command
The terminal says python was not found.
Try python3 or check the installation; this is a setup problem, not a print problem.
What's going on
print("Hello, World!") is the whole program. No class, no main, no ceremony. The comment above it is for people; Python ignores a line starting with #.
print is built in. Matching quotes make the words a string, which is Python's name for text. Either quote style works; opening with one style and closing with the other gives a SyntaxError. A bare identifier such as Hello would instead be looked up as a name.
Save the file as hello.py and run python hello.py, or python3 hello.py on systems that use that command. print writes the text and adds a newline. The quotes are instructions to Python, not part of the displayed greeting.
After this program
If the idea is still fuzzy, read Day 1: Introduction to Python in the Python course. For a short check, try the Python quiz (300 questions). Stay with all 25 basic python programs, or see every topic on Python exercises.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
Do the quote marks appear in the output?
Pick an answer to check it.
Question 2
Does print need an import?
Pick an answer to check it.
Questions people ask
Do the quote marks appear in the output?
No. Quotes mark the string in code; print displays its contents.
Does print need an import?
No. print is a built-in function available when Python starts.
Why does the cursor move to another line?
print adds a newline by default after its arguments.