Strings – Dive Into Python 3
getpython3.com
getpython3.com
(disclosure: I once worked at a company that made tools to port code and data from IBM minicomputers to Unix & MS platforms, and also at the largest [format,] print & mail shop in the US)
It's easier to read text on punchcards in EBCDIC of whatever variation, though.
(Unless you already knew it ten years ago. Back when he wrote "Everything you thought you knew about strings is wrong." it was quite true of most every programmer, and it was thanks to this piece and similar ones, by Spolsky and others, that the information got spread around.
There must be some phrase for this opposite of the "self fulfilling prophecy": the cautionary phrase that causes itself to become false in the future ;-)
EDIT: I know the article went all out about character abstraction, that why i said "some are bytes, some are integers"
For situations where a concrete byte representation is needed, you can get one by encoding the string.
In the past, I've had to deal with horrible mashups of fixed-byte-length columns in flat text files with UTF-8 bolted onto it. In Java, no less. Trying to figure out how to deal with all the edge cases (how do you truncate a string when the boundary is between a "normal" character and a combining character?) was an endless parade of the bizarre. Strings are hard, fundamentally.
d͊
It is composed of two code points: U+0064 and U+034A. The second code point is a combining character. The two code points together form one glyph. The term "character" is confusing because people use different definitions for it, I avoid using it, but the term Unicode code point is very clear.
Python 3's strings is a sequence of code points. The above string is represented like this:
>>> print("d\u034A")
d͊
>>> len("d\u034A")
2
Truncating between the base and combining code points works as expected: >>> "d\u034a"[0]
'd'
>>> "d\u034a"[1]
'͊'As long as you only wander around one of those levels (grapheme, code point, code unit, byte) all is (fairly) easy, but once you deal with multiple levels mistakes almost invariably creep in and you start treating code points as graphemes or code units as code points, etc. Fun source of all kinds of bugs :-)
So yes, text in general is hard. And Han, Hangul and the Japanese scripts are probably among the easiest scripts to support in software :-)
That being said, there are a lot of inaccuracies and even wrong things in that article, which saddens me.
Character is a context in which we read integers and currently we don't use more than a couple hundred thousand of those.
UTF-8 is a data-compression technique taking advantage of the fact that smaller code points are used more often, and the largest ones (which require 5+ bytes, because any compression algorithm expands some inputs) are, currently, not standardized and effectively never used.
> Character is a context in which we read integers and currently we don't use more than a couple hundred thousand of those.
I don't understand this sentence.
> UTF-8 is a data-compression technique taking advantage of the fact that smaller code points are used more often, and the largest ones (which require 5+ bytes, because any compression algorithm expands some inputs) are, currently, not standardized and effectively never used.
It's better to think of UTF-8 and UTF-32 as encodings: their role is to serialize strings into byte sequences and to deserialize byte sequences into strings. Some encodings are more efficient then others in different cases, but that doesn't change their role, and it's not necessary to understand this to avoid Unicode mistakes.
Also I wouldn't say that UTF-8 is a compression scheme. SCSU is one but has its own share of problems. UTF-8 just happens to preserve ASCII compatibility which is an important property for Unix-like systems. Nothing more and nothing less. That is also happens to be more space-efficient for text that consists mostly of ASCII characters is merely a side-effect of that.
I guess if I were German or Spanish (to say nothing of Asian languages), it would be the opposite of compression :-)
I discovered one case today, there may be others. Answer: https://twitter.com/moreati/status/332910618858364928