PYnative

Practice Coding

  • Python ▼
    • Python Tutorials
    • Python Basics
    • Python Interview Q&As
  • Coding Exercises ▼
    • Python Exercises
    • C++ Exercises
    • C Programming Exercises
    • Java Exercises
    • C# Exercises
  • MCQs▼
    • Python MCQ Quizzes
    • Java MCQ Quizzes
    • C++ MCQ Quizzes
  • Compilers▼
    • Online Python Compiler
    • Online C Compiler
    • Online C++ Compiler
    • Online Java Compiler
    • Online C# Compiler
Home » Python » Basics » Your First Python Program: Hello World Explained

Your First Python Program: Hello World Explained

Updated on: December 4, 2026 | Leave a Comment

By tradition, the first program you write in a new language prints the message Hello, World! to the screen. It’s a small program, but it proves that everything works: Python is installed, your editor is set up, and you know how to run code.

In Python, the whole program is a single line. In this tutorial, you’ll write and run it, understand exactly what each part of that line means, and then extend it step by step with comments, variables, user input, and formatted output. You’ll also learn how to read and fix the errors beginners run into most often.

Key terms used in this article:

  • Terminal: A text window where you type commands, such as Command Prompt or PowerShell on Windows and Terminal on macOS and Linux.
  • Function: A named piece of code that does a job, such as print(), which displays text.
  • String: A piece of text in quotation marks, such as "Hello, World!".
  • Variable: A name that stores a value so you can use it later.
  • Error message: Python’s explanation of what went wrong and on which line.

Table of contents

  • Before you begin
  • Write your first Python program
    • Step 1: Create a file
    • Step 2: Run the program
    • Other ways to run it
  • Hello World explained
  • Python vs other languages
  • More ways to use print()
    • Print several values
    • Change the separator and line ending
    • Quotes and special characters
    • Print several lines
  • Add comments to your program
  • Use variables and f-strings
  • Make it interactive with input()
  • Optional: Structure a program with a main() function
  • Common beginner errors and how to fix them
  • Practice exercises
  • Summary

Before you begin

You need two things:

  1. Python 3 installed. Open a terminal and run python --version (Windows) or python3 --version (macOS and Linux). If you see a version number, you’re ready. If not, follow How to Install Python on Windows, macOS, and Linux.
  2. A code editor. Any editor works, including VS Code, PyCharm, or IDLE, which comes with Python. See Setting Up VS Code, PyCharm, and Jupyter for Python.

Note: In this tutorial, terminal commands use python. On macOS and Linux, type python3 instead, unless you’re inside an activated virtual environment.

Write your first Python program

Step 1: Create a file

Create a folder for your Python practice, for example python_projects. Inside it, create a new file named hello.py and type this single line:

print("Hello, World!")Code language: Python (python)

Save the file. A few rules to follow:

  • The file name must end with .py. That tells your computer and editor it’s a Python file.
  • Use lowercase letters, numbers, and underscores in file names, such as hello_world.py. Avoid spaces.
  • Don’t name your file after a Python module, such as random.py or math.py. It can stop that module from being imported later.
  • Type the code exactly, including the lowercase print, the parentheses, and the quotation marks. Python is case-sensitive.

Step 2: Run the program

Open a terminal, move into the folder that contains your file with the cd command, and run the file with Python:

cd python_projects
python hello.pyCode language: Python (python)

Output:

Hello, World!

Congratulations, you’ve written and run your first Python program! The command python hello.py tells the Python interpreter to read hello.py and run every line in it from top to bottom.

Other ways to run it

1. In the interactive shell

Type python on its own in the terminal and press Enter. You’ll see the >>> prompt. Python runs each line as soon as you press Enter, which is perfect for quick experiments:

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

Code typed in the shell isn’t saved. Use a .py file for anything you want to keep.

2. From your editor

  • VS Code: Click the Run Python File button (▷) in the top-right corner.
  • PyCharm: Click the green Run icon (▶) or right-click and choose Run ‘hello’.
  • IDLE: Open the file and press F5, or choose Run › Run Module.

Hello World explained

Let’s break down every part of print("Hello, World!").

PartNameWhat it does
printBuilt-in functionA ready-made function that displays values on the screen (the “standard output”).
( )ParenthesesCall the function. Whatever is inside them is passed to the function as an argument.
"Hello, World!"String literalA piece of text. The quotation marks show where the text starts and ends. They aren’t printed.

So the line means: “call the print function and give it the text Hello, World! to display”.

What is a function?

A function is a named block of code that performs a task. Python comes with many built-in functions you can use without importing anything, such as print(), input(), len(), and type(). To use a function, you write its name followed by parentheses. This is called calling the function.

What is a string?

A string (type str) is a sequence of characters: letters, digits, spaces, and symbols. You can write strings in single quotes or double quotes. Both work the same way:

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

print(type("Hello, World!"))  # <class 'str'>
print(len("Hello, World!"))   # 13 charactersCode language: Python (python)

What is a statement?

Each line of instructions is a statement. In Python, a statement usually ends at the end of the line, so you don’t need a semicolon (;). You also don’t need to wrap the code in a class or a main() function, as you do in Java or C. Python simply runs the file from top to bottom.

If you’re curious about what Python does behind the scenes when you run a file, read How Python Works: Interpreter, Bytecode, and CPython Explained. You don’t need it to keep learning, though.

Python vs other languages

Comparing Hello World across languages shows why Python is considered beginner-friendly. Here is the same program in Java:

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}Code language: Python (python)

And in C:

#include <stdio.h>

int main(void) {
    printf("Hello, World!\n");
    return 0;
}Code language: Python (python)

Both need extra structure, such as a class, a main function, braces, semicolons, and in C, an #include line and a separate compile step. In Python, you write only the line that does the work.

More ways to use print()

print() is the function you’ll use most while learning, so it’s worth knowing its main options.

Print several values

Pass several values separated by commas. print() puts a space between them automatically. You can also join strings with + and repeat them with *:

print("Hello", "World")          # Hello World
print("Hello" + " " + "World")    # Hello World
print("Hi! " * 3)                 # Hi! Hi! Hi!
print("Python", 3, "is fun")      # Python 3 is funCode language: Python (python)

Notice that print() can display numbers too, not just strings.

Change the separator and line ending

  • sep sets what goes between values (a space by default).
  • end sets what goes at the end (a newline by default). Change it to keep the next print() on the same line.
print("Hello", "World", sep="-")

print("Hello", end=" ")
print("World")Code language: Python (python)

Output:

Hello-World
Hello World

Quotes and special characters

To include a quotation mark inside a string, use the other type of quote around the string, or put a backslash (\) before it. A backslash also starts special escape sequences, such as \n for a new line and \t for a tab.

print("It's a great day")         # single quote inside double quotes
print('He said "Hi"')              # double quotes inside single quotes
print("She said, \"Hello!\"")      # escaped double quotes
print("Name:\tJessa")              # \t inserts a tab
print("C:\\Users\\Jessa")          # \\ prints one backslashCode language: Python (python)

Output:

It's a great day
He said "Hi"
She said, "Hello!"
Name:	Jessa
C:\Users\Jessa

Print several lines

Use \n inside a string, call print() with no arguments to print an empty line, or use a triple-quoted string, which keeps the line breaks exactly as you type them:

print("Line 1\nLine 2")
print()
print("""Hello,
World!""")Code language: Python (python)

Output:

Line 1
Line 2

Hello,
World!

Here’s a slightly bigger program that combines these ideas to draw a box around the message:

print("*" * 20)
print("*   Hello, World!  *")
print("*" * 20)Code language: Python (python)

Output:

********************
*   Hello, World!  *
********************

Add comments to your program

A comment is a note for people reading your code. Python ignores everything after a # on a line. Use comments to explain why code does something.

# My first Python program
# It prints a greeting to the screen

print("Hello, World!")  # display the message

# print("This line won't run")Code language: Python (python)

The last line is “commented out”, so Python skips it. Commenting out a line is a quick way to disable code temporarily. Learn more in Python Comments.

Use variables and f-strings

A variable is a name that stores a value so you can use it later. Create one with the = sign. To insert variables into a message, use an f-string: put f before the opening quote and wrap each variable in curly braces { }.

name = "Jessa"
language = "Python"
year = 2026

print("Hello,", name)
print(f"My name is {name}.")
print(f"I am learning {language} in {year}.")Code language: Python (python)

Output:

Hello, Jessa
My name is Jessa.
I am learning Python in 2026.

f-strings are the clearest way to build messages from values, and you’ll use them constantly.

Make it interactive with input()

The built-in input() function shows a prompt, waits for the user to type something and press Enter, and returns what they typed as a string. Let’s make Hello World greet the user by name:

name = input("Enter your name: ")
print(f"Hello, {name}! Welcome to Python.")Code language: Python (python)

Output:

Enter your name: Jessa
Hello, Jessa! Welcome to Python.

Your program now responds to the person running it. That’s the basic pattern of most programs: take input, process it, and produce output.

Optional: Structure a program with a main() function

Optional: You can skip this section for now. You don’t need this pattern for your first programs, but you’ll see it in many Python files, so it helps to recognize it.

As programs grow, Python developers often put the main code inside a function and call it like this:

def main():
    print("Hello, World!")


if __name__ == "__main__":
    main()Code language: Python (python)

Output:

Hello, World!
  • def main(): defines a function named main. The indented line below it is the function’s body.
  • __name__ is a special variable. Python sets it to "__main__" when you run the file directly.
  • The if check means main() runs when you run the file, but not when another file imports it. This keeps your code reusable.

Common beginner errors and how to fix them

Errors are a normal part of programming. Python’s error messages tell you the file, the line number, the type of error, and often a suggestion. Always read the last line first. Here are the mistakes beginners make most often with Hello World.

1. Capital letter in print: NameError

Print("Hello, World!")Code language: Python (python)
Traceback (most recent call last):
  File "hello.py", line 1, in <module>
    Print("Hello, World!")
    ^^^^^
NameError: name 'Print' is not defined. Did you mean: 'print'?

Fix: Python is case-sensitive. Use lowercase print.

2. Missing closing quote: SyntaxError

print("Hello, World!)Code language: Python (python)
  File "hello.py", line 1
    print("Hello, World!)
          ^
SyntaxError: unterminated string literal (detected at line 1)

Fix: Every string needs matching quotes at both ends, and they must be the same type ("..." or '...'). Also check that your editor didn’t insert curly “smart quotes” (“ ”), which Python doesn’t accept.

3. Missing closing parenthesis: SyntaxError

print("Hello, World!"Code language: Python (python)
  File "hello.py", line 1
    print("Hello, World!"
         ^
SyntaxError: '(' was never closed

Fix: Add the closing ).

4. Python 2 syntax: SyntaxError

print "Hello, World!"Code language: Python (python)
  File "hello.py", line 1
    print "Hello, World!"
    ^^^^^^^^^^^^^^^^^^^^^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?

Fix: In Python 3, print is a function and needs parentheses. Old tutorials written for Python 2 use this outdated form.

5. Unexpected space at the start of a line: IndentationError

print("Hello")
    print("World")Code language: Python (python)
  File "hello.py", line 2
    print("World")
IndentationError: unexpected indent

Fix: In Python, indentation has meaning. Only indent lines that belong to a block, such as the body of a function, loop, or if statement. Top-level lines start at the very left.

6. Wrong file name or folder: “can’t open file”

python: can't open file '/Users/jessa/python_projects/helo.py': [Errno 2] No such file or directory

Fix: Check the spelling of the file name, and make sure your terminal is in the folder that contains the file. Run dir (Windows) or ls (macOS and Linux) to list the files in the current folder.

7. Running a file from inside the Python shell

If you type python hello.py at the >>> prompt, you’ll get a SyntaxError, because the shell expects Python code, not terminal commands. Type exit() to leave the shell first, then run the command in the terminal.

Practice exercises

Try these small tasks to practice what you’ve learned. Try each one yourself before looking at the solution.

Exercise 1: Print your name, your city, and your favorite food on three separate lines using a single print() call.

# Solution
print("Jessa\nPune\nPizza")Code language: Python (python)

Exercise 2: Print the words Python, is, and fun separated by ***, so the output is Python***is***fun.

# Solution
print("Python", "is", "fun", sep="***")Code language: Python (python)

Exercise 3: Ask the user for their name and favorite programming language, then print a message such as Hi Jessa, Python is a great choice!

# Solution
name = input("What is your name? ")
language = input("What is your favorite programming language? ")
print(f"Hi {name}, {language} is a great choice!")Code language: Python (python)

For more practice, solve the Python Input and Output Exercise.

Summary

  • The Python Hello World program is one line: print("Hello, World!"). Save it in a file ending in .py and run it with python hello.py (python3 on macOS and Linux).
  • print is a built-in function. The parentheses call it, and "Hello, World!" is a string argument passed to it.
  • Python needs no semicolons, no class, and no main() function. It runs the file from top to bottom.
  • Use commas to print several values, sep and end to control spacing, and escape sequences such as \n and \t for new lines and tabs.
  • Comments start with #. Variables store values, and f-strings such as f"Hello, {name}" insert them into text. input() reads text from the user.
  • The if __name__ == "__main__": pattern runs code only when the file is run directly.
  • Read the last line of an error message first. Most beginner errors come from capital letters, missing quotes or parentheses, Python 2 syntax, unexpected indentation, or running the command from the wrong folder.

Also, See:

  • Python Variables
  • Python Data Types
  • Python Input: Take Input from User

Filed Under: Python, Python Basics

Did you find this page helpful? Let others know about it. Sharing helps me continue to create free Python resources.

TweetF  sharein  shareP  Pin

About Vishal

I’m Vishal Hule, the Founder of PYnative.com. As a Python developer, I enjoy assisting students, developers, and learners. Follow me on Twitter.

Related Tutorial Topics:

Python Python Basics

All Coding Exercises:

C Exercises
C++ Exercises
Python Exercises
Java Exercises
C# Exercises

Python Exercises and Quizzes

Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.

  • 15+ Topic-specific Exercises and Quizzes
  • Each Exercise contains 25+ questions
  • Each Quiz contains 25 MCQ
Exercises
Quizzes

Leave a Reply Cancel reply

your email address will NOT be published. all comments are moderated according to our comment policy.

Use <pre> tag for posting code. E.g. <pre> Your entire code </pre>

In: Python Python Basics
TweetF  sharein  shareP  Pin

  Python Tutorials

  • Get Started with Python
  • Python Statements
  • Python Comments
  • Python Keywords
  • Python Variables
  • Python Operators
  • Python Data Types
  • Python Casting
  • Python Control Flow statements
  • Python For Loop
  • Python While Loop
  • Python Break and Continue
  • Python Nested Loops
  • Python Input and Output
  • Python range function
  • Check user input is String or Number
  • Accept List as a input from user
  • Python Numbers
  • Python Lists
  • Python Tuples
  • Python Sets
  • Python Dictionaries
  • Python Functions
  • Python Modules
  • Python isinstance()
  • Python OOP
  • Python Inheritance
  • Python Exceptions
  • Python Exercise for Beginners
  • Python Quiz for Beginners

 Explore Python

  • Python Tutorials
  • Python Exercises
  • Python Quizzes
  • Python Interview Q&A
  • Python Programs

All Python Topics

  • Python Basics
  • Python Exercises
  • Python Quizzes
  • Python File Handling
  • Python Date and Time
  • Python OOP
  • Python Random
  • Python Regex
  • Python Pandas
  • Python Databases
  • Python MySQL
  • Python PostgreSQL
  • Python SQLite
  • Python JSON

About PYnative

PYnative.com is a platform built to help developers learn by doing. Solve coding exercises, take MCQs, and run code directly in built-in online compilers.

Follow Us

To get New Python Tutorials, Exercises, and Quizzes

  • Twitter
  • Facebook
  • Sitemap

Explore Python

  • Learn Python
  • Python Basics
  • Python Databases
  • Python Exercises
  • Python Quizzes
  • Online Python Code Editor
  • Python Tricks

Coding Exercises

  • C Exercises
  • C++ Exercises
  • Python Exercises
  • Java Exercises
  • C# Exercises

MCQ Quizzes

  • Python MCQs
  • Java MCQs
  • C++ MCQs

Legal Stuff

  • About Us
  • Contact Us

We use cookies to improve your experience. While using PYnative, you agree to have read and accepted our:

  • Terms Of Use
  • Privacy Policy
  • Cookie Policy

Copyright © 2018–2026 pynative.com