Hello there! If you’ve found your way here, you’re likely on the brink of diving into the wonderful world of programming, and specifically, Python programming. I remember my early days with Python, and I’m thrilled to guide you how to print Hello World in Python through steps.
Today, we’re starting with something that might sound simple but is a rite of passage for every programmer: printing “Hello World!” to the screen.
Understanding Python
Before we jump into writing our code, it’s important to understand a little about Python. Python is a versatile, high-level programming language that’s known for its simplicity and readability.
What that means for you, as a beginner, is that it’s an excellent starting point. Its syntax is clear, and you don’t need to know a lot of complex rules to start writing your first program.
How to Print Hello World in Python: Setting Up Your Environment
Before you can write and run a Python script, you’ll need to have latest Python installed on your computer. The process is straightforward:
- Visit the official Python website at python.org.
- Navigate to the “Downloads” section and select the version suitable for your operating system.
- Follow the installation instructions. Ensure you tick the option “Add Python to PATH” during the installation. This will make running Python much simpler!
If you are a Chromebook user follow the steps here to install Python.
Writing Your First “Hello World” Program
Alright, let’s get our hands dirty and start coding! Once you have Python installed, you have a couple of ways to run your first program.
Method 1: Using the Python Interactive Shell
- Open your terminal (Command Prompt on Windows, Terminal on macOS/Linux).
- Type
python
and press Enter. You should see the Python prompt (>>>
). - Now, simply type:
print("Hello World!")
Press Enter, and voilà! You’ll see “Hello World!” printed right below your command.
Method 2: Using a Python Script
- Open a text editor (like Atom, Notepad on Windows or TextEdit on macOS).
- Type in:
print("Hello World!")
- Save the file with a
.py
extension. For example, you could save it ashello.py
. - Now, go back to your terminal or command prompt, navigate to the directory where you saved your file using the CD command.
- Type following command and press enter. There you have it, “Hello World!” should appear on your screen.
python hello.py
A Few Variations
Just to get a little playful and give you a taste of how versatile even a simple command like print
can be, let’s try a couple of variations:
1. Printing with a Twist
print("Hello", "World!", sep="-")
This will print: Hello-World!
2. Printing Multiple Times
for i in range(3): print("Hello World!")
This will print “Hello World!” three times in a row.
3. Getting User Input and Personalizing the Greeting
name = input("What's your name? ") print(f"Hello, {name}!")
This one prompts you to enter your name and then greets you personally!