This post originated from an RSS feed registered with Python Buzz
by Thomas Guest.
Original Post: A useful octal escape sequence
Feed Title: Word Aligned: Category Python
Feed URL: http://feeds.feedburner.com/WordAlignedCategoryPython
Feed Description: Dynamic languages in general. Python in particular. The adventures of a space sensitive programmer.
they aren’t much use, not when you’ve got hexademical and binary literals.
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.
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.