A few months ago, I was standing in line at a small coffee shop. Instead of the usual printed menu, they had a little black-and-white square taped to the counter. A QR code. People just scanned it with their phones, and voilà, the menu popped up instantly.

I realized then: this simple pattern of dots is everywhere. Restaurants, event tickets, Wi-Fi access, payments, you name it. And yet, even as a developer, I wasn’t entirely sure how QR codes actually worked. I’d scanned them countless times, but I couldn’t have told you what was really going on behind the scenes.

That bugged me. Curiosity has a way of sneaking up on you when you write code for a living, and I couldn’t shake the urge to figure it out. So I decided to dig in. Spoiler: it’s both simpler and cooler than I expected.


What’s Inside a QR Code?

When I finally sat down to figure out how QR codes work, I was surprised by how elegant the design really is. At its core, a QR code, short for Quick Response code, is simply a way of storing data in a two-dimensional grid.

Traditional barcodes are like reading a sentence: left to right, one line at a time. But QR codes are more like a page of text, where your phone’s camera can read information both horizontally and vertically. That extra dimension is what allows QR codes to pack in so much more information, sometimes thousands of characters instead of just a handful of numbers.

The “magic” of a QR code really comes down to a few clever building blocks:

  • Data modules: those tiny black and white squares that hold the actual information, whether it’s a link, some text, or even Wi-Fi credentials.
  • Position markers: the three large squares in the corners that act like anchors, telling your phone, “Here’s the orientation, here’s how to read me.”
  • Error correction: a safety net that makes the code surprisingly robust. Even if part of the QR code is smudged, folded, or covered up, your phone can often still piece together the missing bits and recover the data.

That’s the bird’s-eye view. But what’s really happening inside that grid is even more fascinating:

Inside a QR Code

Encoding the Data

Let’s say you want to put the text Hello inside a QR code. That text is first converted into numbers using a standardized system (ASCII or UTF-8 encoding). Then those numbers are turned into binary, just ones and zeros.

Those ones and zeros are arranged in a zigzag pattern across the grid, filling in the little squares. Black squares usually represent a 1, white squares a 0. So when your phone scans the code, it’s basically reading a binary message hidden in plain sight.

This is similar to how I approached transforming images into ASCII art with Python, taking something human-readable and breaking it down into patterns a computer can understand.

Error Correction Magic

Now, here’s the clever part: QR codes use something called Reed–Solomon error correction. It’s the same math that protects scratches on your old DVDs or keeps satellite transmissions readable despite interference.

What it means in practice is that QR codes can be up to 30% damaged and still scan perfectly. Ever seen a QR code with a company logo slapped in the middle? That’s possible because of this error correction, your phone can fill in the missing puzzle pieces even if some of the squares are gone.

And that’s what I love about Python, too: the “magic” is there, but once you understand what’s happening under the hood, you can use it in smarter, more powerful ways. If that idea excites you, you’ll enjoy my book Python’s Magic Methods, where I show how to level up your classes with __call__, __enter__/__exit__, custom hashing, dynamic attribute access, and more. It’s short, sharp, and packed with practical examples.

Because the data is just binary, QR codes can store more than URLs. They can hold Wi-Fi passwords, contact info, payment requests, or even small images. Your phone just needs to know how to interpret the payload once it decodes it.

What struck me was how much thought went into making something so simple to use. To the average person, it’s just a square that opens a link. But under the hood, it’s a brilliantly compact system for encoding data, like a little puzzle that your phone instantly solves.

In other words, that small grid taped to the coffee shop counter wasn’t just a random pattern of dots. It was a tiny container holding a URL, wrapped in a design that made it both readable and resilient.


Making a QR Code in Python

Now, here’s the best part: you don’t need to understand all the math behind QR encoding to actually use it. Someone else has already done the heavy lifting for us, and Python (of course) has a library that makes it almost laughably simple.

With just a few lines of Python, I wrote the script, hit run, and suddenly I had a crisp little PNG file sitting on my desktop. I scanned it with my phone, and boom, it opened my blog.

Here’s the exact code I used, with the excellent qrcode library: