what is difference between 'aa' and '\xaa' and which chapter of python documentation will cover this topic? thanks in advance.
A:
That's unicode character escaping. See "Unicode Constructors" on PEP 100
Jake Wharton
2010-04-20 03:28:02
No it isn't. It's for defining a specific byte in a `str`, not for making a unicode code point, which is done with the `u'\u...` notation.
Mike Graham
2010-04-20 03:48:45
@Mike, @Jake: It's for BOTH. '\xaa' is a str object. u'\xaa' is a unicode object. `print repr(unichr(170))` produces `u'\xaa'`
John Machin
2010-04-20 03:54:03
Whoops. Grossly misread. Thanks for the heads up.
Jake Wharton
2010-04-20 04:02:50
@Mike Graham, it's **IronPython** for goodness sake -- otherwise-unadorned strings are **unicode** (just like in Jython and Python 3). _Read the tags_! (Also goes for John Machin, but I already mentioned that to him wrt his comment on _my_ answer).
Alex Martelli
2010-04-20 04:08:48
Oops. I seem not to have noticed the IronPython tag. *blush*. The concepts in my comment are still pretty pertinent—`\x` and `\u` remain somewhat different things.
Mike Graham
2010-04-20 13:08:35
+3
A:
The leading \x
escape sequence means the next two characters are interpreted as hex digits for the character code, so \xaa
equals chr(0xaa)
, i.e., chr(16 * 10 + 10)
-- a small raised lowercase 'a'
character.
Escape sequences are documented in a short table here in the Python docs.
Alex Martelli
2010-04-20 03:28:35
chr(170) can be interpreted without reference to a particular encoding only in the context of Python 3.X, and it's actually a "feminine ordinal indicator" ... a peculiarity of Spanish orthography, along with its sibling U+00BA "masculine ordinal indicator".
John Machin
2010-04-20 03:49:05
@John: in IronPython, like in Python 3, `'\xaa'` is a Unicode string. Didn't you happen to see the "IronPython" tag on the question? (btw, I resent your assertion that, e.g., `2ª` for "second [[feminine]]" is "a peculiarity of Spanish orthography" -- I mean, for example, what's Italian, my native language, chopped liver?!).
Alex Martelli
2010-04-20 04:06:27
@Alex: (1) Nah, reading tags slows one down. (2) My native tongue is the SEQ dialect of Strine, which doesn't have such fancy thingies, so I relied on a supposed authority (http://unicode.org/charts/PDF/U0080.pdf). As for your strange question, I don't know whether Italian is chopped liver or not.
John Machin
2010-04-20 04:31:10