Why do numbers in a string become "x0n" when they are preceded by a backslash?

I did some backslash experiments in the Python 3.4 shell and noticed something rather strange.

>>> string = "\test\test\1\2\3"
>>> string
'\test\test\x01\x02\x03'
>>> string = "5"
>>> string
'5'
>>> string = "5\6\7"
>>> string
'5\x06\x07'

As you can see in the above code, I defined the variable string as "\test\test\1\2\3". However, when I typed stringin the console, instead of printing "\test\test\1\2\3", it printed "\test\test\x01\x02\x03". Why is this happening and what is it used for?

+4
source share
2 answers

Python \ escape-. \n , \t .. , \uhhhh 4- \Uhhhhhhhh 8- .

. String Bytes Literals, escape-.

Python ( repr() ), Python . Python, , .

, Python escape-, . , , , \xhh , , \c ( \n).

, escape- \ooo . . \xhh :

>>> '\20' # Octal for 16
'\x10'

\t :

>>> print('\test')
    est

, t; est , .

\ , :

>>> '\\test\\1\\2\\3'
'\\test\\1\\2\\3'
>>> print('\\test\\1\\2\\3')
\test\1\2\3
>>> len('\\test\\1\\2\\3')
11

, ! , Python, . print(), , ( ) , , , , 11 , 15.

. , , , . . - , ; :

>>> r'\test\1\2\3'
'\\test\\1\\2\\3'

, : , Windows, ; API- :

>>> 'C:/This/is/a/valid/path'
'C:/This/is/a/valid/path'
+8

string = "\test\test\1\2\3"

Python , , ( "\ t" ), "e", "s" .. Python , , 1, 2 3, "\ 1", "\ 2" "\ 3".

+2

All Articles