Printing a cheery “Hello World!” to standard output marks the starting point for many programmers. In this article, we’ll take a look at some features of this seemingly trivial piece of code. We’ll go over Hello World’s origins, how its usage has changed with the evolution of the Python language, why it looks more complex in other languages and which alternatives you can use if you get bored of Hello World.

A Short History of Hello World

The first mention of a Hello World program dates back to 1972 when Brian Kernighan wrote it to illustrate the usage of external variables in the B programming language. In B, variables of the char type could not exceed four characters, so the message had to be broken up into three segments (“hell,” “o, w” and “orld”), which were then passed on to the function.

The B manual, however, was an internal document at Bell Laboratories. But the coding example gained notoriety when it was used in the book “The C Programming Language,” co-written by Kernighan and Dennis Ritchie, the author of C. Published in 1978, the book became an instant classic and came to be simply called “K&R,” after its authors. Hello World, for its part, eventually lost its comma and gained an exclamation point.

Hello World in Python

The Console

To program a “Hello World” example, you need to be familiar with the Python console, also called the shell. It’s a straightforward, text-based interface that accepts your code and executes it as soon as you press Enter.

The print() Function

A variant of the print() function exists in nearly every language, often under different names. In bash for instance, it’s called echo, and in Ruby it’s puts. The function takes the given input and prints it to the screen. print() may be used for user interaction with the console or during debugging, where developers use it to quickly see what’s happening in their buggy code.

Hello World in Python 3

Version 3.0 of Python, also known as Python 3000 or Py3k, has been the de facto language standard as of January 1, 2020, when support for Python 2 stopped. In Python 3, programming a “Hello World” message is as easy as typing print(‘Hello World’) into the Python console:

>>> print(‘Hello World’)

Hello World

Note that single and double quotes may be used interchangeably in Python.

Hello World in Python 2

Remember that we called print a function in Python 3? Well, that wasn’t the case for Python 2. The language’s inventor, Guido van Rossum, has stated that “print should’ve been a function” rather than a statement, as in Python 2. This shortcoming was one that Python 3, which is not backwards-compatible with Python 2, sought to fix.

While Python functions take arguments (the references in brackets), statements do not. In Python 2, you would have to write print ‘Hello World’ to execute our little program. Notice the space instead of the brackets. However, as of version 2.6, you could use the print() function by doing from __future__ import print_function.

Migration From 2 to 3

Backwards-compatibility means that even if you have a newer version of a programming language installed, you can still use it to run code written for an older version. Many companies were much slower to adapt their code to the new version than the Python development team had expected. This led to code breaking, for instance where the old print statement was used. 

If that should happen to you, you could use a library like six or 2to3 to automatically port old Python code to the new version.

How to Write a Hello World Script in Python

Interacting with the console is fun, but short-lived. If you want to reuse your code, you’ll have to write a script and call it from the console. Let’s look at the steps involved in creating your first Hello World program.

Install Python

If you don’t yet have Python 3 installed, it’s probably time to change that! Check out this cool tutorial with detailed instructions on how to install Python on Windows, Linux or MacOS.

Open a Text Editor

Your text editor will become your best friend — if it’s well-suited to your needs, that is. There are many different editors, with varying functionalities and learning curves. Atom and Sublime are solid picks for beginners.

Write the Script

This step consists of nothing more than typing print(‘Hello World’) into your editor, and saving the file under a unique name, such as helloworld.py (.py being the file extension that marks this as a Python script.)

Run the Script From the Console

Open the terminal (not the Python shell) and type the following command (note that the “$” symbol is used conventionally to indicate the beginning of the command-line prompt):

$ python helloworld.py

If your default Python version is version 2, you might have to specify that you want to use the newest version by running Python 3 explicitly:

$ python3 helloworld.py

And that’s it! From here, you can start experimenting in various ways. You might want to experiment with different outputs and perhaps even inputs, or look into adding a shebang line at the top of your script if you are a Linux or macOS user. Or you might want to move from your text editor to an intelligent development environment (IDE) with greater functionality.

Hello World in Other Languages

One of the reasons why “Hello World” became so popular was that, despite its simplicity, it could be used to show some basic characteristics (and test some functionalities) of a language. If you’re used to Python, it might be beneficial to look at “Hello World” in other languages.

Java and C

In Java for instance, the program is much longer than in Python. For starters, Java is a statically typed language, meaning that the type of a variable needs to be declared explicitly. In Python, by contrast, the presence of quotation marks suffices to type a variable as a string.

Here’s the Java example:

public class HelloWorld {

public static void main(String[] args) {

System.out.println(“Hello world!”);

}

}

In C ( the language in which Python was written), not only do you need to declare the variable type, but you also have to import the stdio package — the standard input and output library — in the script’s header.

#include <stdio.h>

int main() {

printf(“Hello world\n”);

}
Note how in C the newline character “\n” has to be explicitly included in the string, whereas in Python the print() function automatically produces a new line every time it’s called.

TTHW

But why do we have to write so much more code in some languages to accomplish the same thing as we did with Python’s print(“Hello World!”)?  In reality, a lot of work happens under the hood in the Python example, too. We just don’t see it. Python is known as a high-level programming language — meaning that its syntax abstracts away from a lot of the implementation details. 

In fact, when you look at Assembly Languages — which are just one level above raw machine code — a simple Hello World program consists of at least seven lines. This is known as the Time to Hello World (TTHW) principle. The more time you need to write your Hello World program, the more low-level the language you’re learning. For a humorous take on the concept, take a look here.

Alternatives to Hello World

There’s a lot to like about Hello World. It’s simple and it does the job of making sure you have your programming pipeline in place to start writing more complex code. Most importantly, it’s part of a long tradition that makes it feel like an initiation into a language. But it’s also a little bit boring, and in some cases, it cannot really be applied. What are the alternatives?

To take the case of a microcontroller, which does not have a graphical user interface, writing a simple program to make an LED blink has been described as an equivalent to Hello World. If you want to make your own Python Hello World program more interesting, consider printing your string in a different language, or trying out a slightly more interesting programming exercise, such as 99 Bottles of Beer, which illustrates the use of for-loops. 

Learn More

Interested in going beyond the Hello World exercise and becoming fluent in Python? Learn more about our Intro to Programming course.

Start Learning