What is difference between 'aa'
and '\xaa'
? What does the \x
part mean? And which chapter of the Python documentation covers this topic?
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.
The \x means it's a hex character escape. So \xeb would mean character eb in hex, or 235 in decimal.
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)
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 ( [] ).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With