A useful octal escape sequence

2009-08-09, Comments

Previously I grumbled about octal integer literals, suggesting:

  1. they aren’t much use, not when you’ve got hexademical and binary literals.
  2. they’re risky. As Doug Napoleone noted in a comment, 011 is all too easily read as eleven rather than nine.

Python 3 attempts to patch the readability issue: octal nine must be written as 0o11 or 0O11. Choose your fonts with care, O and 0 look similar!

>>> 0o11, 0O11
(9, 9)

Octal numbers can also appear in escape sequences embedded in string and bytes literals. The syntax hasn’t changed for Python 3 and the rules are as in C: the escape sequence \OOO embeds a character/byte with octal value OOO. Up to three octal digits are allowed in an octal escape sequence.

Smart mollusc Smart mollusc Smart mollusc

Like octal integers, these octal escape sequences may appear to be of limited use — a syntactic oddity rarely seen in the wild — but in fact there’s one particular use case which is so common we don’t even notice it.

char const terminator = '\0';