What is the difference between \x
and \u
escape sequences in python? (Apart from the fact that \x
uses the syntax \xXX
and \u
uses \uXXXX
). print('\xa5')
gives the output as '¥'
in script mode and so does print('\u00a5')
, so how is one different from the other, apart from the syntax used?
Asked
Active
Viewed 329 times
0

Pavel Bucek
- 5,304
- 27
- 44

incomplet_
- 318
- 2
- 10
-
The answer to this question does not help me directly with my problem, however the link provided in the answer [link](https://docs.python.org/2/reference/lexical_analysis.html#string-literals) was indeed very helpful, thank you @ChrisStillwell for pointing out this question. – incomplet_ Sep 16 '15 at 07:29
1 Answers
0
The most important difference is that \uXXXX
accepts 4 hexadecimal digits and is therefore suitable for higher numbers (and therefore can be used to refer to special characters that are not in ASCII or your current code page). It can therefore only be used in unicode strings:
u'\u0123'
The older \xXX
can be used in both unicode strings and str
strings, but only for code points up to 255:
u'\u0123\x20'
'\x20'

Waggili
- 371
- 1
- 7