views:

95

answers:

2

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
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
@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
Whoops. Grossly misread. Thanks for the heads up.
Jake Wharton
@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
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
+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
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
@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
@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