One lesson I try to teach students all the time is that there are many different ways to solve a problem. To prove that, I wanted to try implementing one of the most basic programs that you can in any programming language, Hello World, in as many different ways as possible.
Table of Contents
Defining the Problem: Hello World
Hello World is a common enough introductory program that it probably doesn’t need to be explained, but let me take a moment to do it anyway.
In programming, we often want to be able to perform some computation and share that result with the user. There are many ways to share results, but the two main ways are through a command line interface (CLI) or a graphical user interface (GUI). Because GUIs require a bit more knowledge of programming to build, we often start programming from the CLI.
From the CLI, the main way of interacting with a program is through the input and output of text on the command line. For the purposes of Hello World, we won’t concern ourselves with input. However, we do want to generate some output—specifically the phrase “Hello, World!”.
In short, a Hello World program in Python should work as follows:
$ python hello_world.py Hello, World!
In the first line, we run the Hello World program, and the second line shares the output. That’s it! Funnily enough, there are many different ways to accomplish this behavior. Let’s take a look at few in the next section.
Solutions to Hello World
To solve this problem, we’ll be looking at the typical solution(s) first. Then, we’ll move through to more obscure solutions. Keep in mind that I’m not suggesting you use any of the more obscure solutions but rather that even simple problems can have many solutions. Feel free to treat the more ridiculous solutions like a 5 Minute Crafts video (i.e., they’re not actual practical).
The Vanilla Hello World: Print()
If you’ve ever written a line of Python before, you’re probably already familiar with the print()
function. In short, it allows us to write any text we’d like directly to the command line as follows:
print("Hello, World!")
That’s it! This single line will solve the problem of Hello World. Let’s see how wacky we can get as we keep going.
The Retro Hello World: Print
At this point, Python is somewhat of an old language. As a result, there are probably folks using old versions of it. As recently as version 2, Python had support for a print()
function that didn’t require the parentheses:
print "Hello, World!"
These days, however, you’ll get a syntax error:
>>> print "hi" SyntaxError: Missing parentheses in call to 'print'. Did you mean print("hi")?
That said, if you’re using Python 2, this might still be the way to go.
The Chad Hello World: Write
In Python, there are also some truly goofy ways to print to the command line. For example, you can pull the standard out stream directly from the sys
library:
import sys sys.stdout.write("Hello, World!\n")
If you’re using a REPL, you might need to be careful with this solution since write()
also returns the length of the string.
The Gigachad Hello World: Log
If you’re familiar with logging, then you already know where this next solution is going. In this solution, we’re going to use the logging
library to print Hello World:
import logging logging.info("Hello, World!")
Unfortunately, this alone isn’t enough to get it done. We also need to configure the logger a bit:
import logging logging.basicConfig(level=logging.INFO) logging.info("Hello, World!") # Prints "INFO:root:Hello, World!"
And we can go to the extreme to remove all of the useful logging bits:
import logging logging.basicConfig(level=logging.INFO, format="%(message)s") logging.info("Hello, World!") # Prints "INFO:root:Hello, World!"
Now that’s a totally reasonable way to print.
The Sigma Hello World: Exceptions
Another silly way to get text to the console is to raise an exception:
raise Exception("Hello, World!")
This alone is going to print a bunch of garbage, so why don’t we throw the error and catch it ourselves:
try: raise Exception("Hello, World!") except Exception as e: print(str(e))
Now, this one is pretty amazing to me because we could just print “Hello, World!” directly. Instead, we took the long way there. To me, this seems like a pretty awesome trick to add to my obfuscation rulebook.
Challenge
Whenever I come up with these kinds of articles, I end up down a pretty wild rabbit hole that explores the depths of my favorite programming language. What started as a silly concept (i.e., how many different ways can you reasonably write Hello World in Python?) turned into quite the creative excursion. As a result, I’d love for you to try creating your own solutions using #RenegadePython on Twitter.
In the meantime, why not check out some of these related articles:
- Breaking Down a Hello World Discord Bot in Python
- How to Capitalize a String in Python: Upper(), Capitalize(), And More
- IndexError: String Index out of Range
Likewise, here are some helpful Python resources (#ad):
- Effective Python: 90 Specific Ways to Write Better Python
- Python Tricks: A Buffet of Awesome Python Features
- Python Programming: An Introduction to Computer Science
Finally, you can show even more support by checking out my list of ways to grow the site. Otherwise, have a good one!
Recent Posts
Recently, I was thinking about the old Pavlov's dog story and how we hardly treat our students any different. While reflecting on this idea, I decided to write the whole thing up for others to read....
In the world of programming languages, expressions are an interesting concept that folks tend to implicitly understand but might not be able to define. As a result, I figured I'd take a crack at...