You can embed absolute binary values into the characters of a string.
The following code has a five-character string that embeds two characters with binary zero values:
s = 'a\0b\0c' print( s ) print( len(s) )
Here's a string that is all absolute binary escape codes-a binary 1 and 2 (coded in octal), followed by a binary 3 (coded in hexadecimal):
s = '\001\002\x03' print( s ) print( len(s) )
Python displays non-printable characters in hex, regardless of how they were specified.
You can freely combine absolute value escapes and the more symbolic escape types.
The following string contains the characters "test", a tab and newline, and an absolute zero value character coded in hex:
S = "s\tp\na\x00m" print( S ) print( len(S) )
If Python does not recognize the character after a \ as being a valid escape code, it simply keeps the backslash in the resulting string:
x = "C:\py\code" # Keeps \ literally (and displays it as \\) print( x ) print( len(x) )
To code literal backslashes explicitly such that they are retained in your strings, double them up (\\ is an escape for one \) or use raw strings.