Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does a leading `\x` mean in a Python string `\xaa`

What is difference between 'aa' and '\xaa'? What does the \x part mean? And which chapter of the Python documentation covers this topic?

like image 523
Haiyuan Zhang Avatar asked Apr 20 '10 03:04

Haiyuan Zhang


People also ask

What does b in front of string mean Python?

The b" notation is used to specify a bytes string in Python. Compared to the regular strings, which have ASCII characters, the bytes string is an array of byte variables where each hexadecimal element has a value between 0 and 255.

What does \x mean in bytes?

The \x means it's a hex character escape. So \xeb would mean character eb in hex, or 235 in decimal.

What does * string mean in Python?

The * operator can be used to repeat the string for a given number of times. # Python String Operations str1 = 'Hello' str2 ='World!' # using + print('str1 + str2 = ', str1 + str2) # using * print('str1 * 3 =', str1 * 3)

How do you refer to a string in Python?

String Indexing This process is referred to as indexing. In Python, strings are ordered sequences of character data, and thus can be indexed in this way. Individual characters in a string can be accessed by specifying the string name followed by a number in square brackets ( [] ).


1 Answers

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.

like image 57
Alex Martelli Avatar answered Oct 09 '22 07:10

Alex Martelli