Chapter 17. Text Data
I live for the text. It’s my job.
Ian McKellen
You saw the basics of Python strings in Chapter 4. Now it’s time to dig deeper into strings and text data.
Text Strings: Unicode
Python 3 strings are Unicode character sequences, not bytearrays. This is, by far, the single largest language change from Python 2.
All the text examples in this book thus far have been plain old ASCII. ASCII was defined in the 1960s, before mullets roamed the earth. Computers then were the size of refrigerators, and only slightly smarter.
As I mentioned way back in Chapter 2, the basic unit of computer storage is the byte, which can store 256 unique values in its eight bits. For various reasons, ASCII used only seven bits (128 unique values): 26 uppercase letters, 26 lowercase letters, 10 digits, some punctuation symbols, some spacing characters, and some nonprinting control codes.
Unfortunately, the world has more letters than ASCII provides. You could have a hot dog at a diner, but never a Gewürztraminer at a café.1 Many attempts have been made to cram more letters and symbols into eight bits, and you’ll see them at times.
Here are just a couple of those:
-
Latin-1, or ISO 8859-1
-
Windows code page 1252
Each of these uses all eight bits, but even that’s not enough, especially when you need non-European languages. Unicode is an ongoing international standard to define the characters of all the world’s languages, plus symbols from mathematics and other fields. And emojis!
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access