I want to run the below command in python. I need to print the '\0000'. I try different ways to print it. I got '\x00' or '\000'. How do generate the string '\000' in python? thank you so much.
gsutil cp xxxxx.csv - | tr -d '\000' | gsutil cp - xxxxx.csv
>>> remove_string='''\000'''
>>> remove_string
'\x00'
>>> remove_string='\\000'
>>> remove_string
'\\000'
>>> remove_string=r'\\000'
>>> remove_string
'\\\\000'
>>> remove_string=r'\000'
>>> remove_string
'\\000'
CodePudding user response:
A string literal is the text you type into a program that python compiles into a str
object. Python treats the backslash character \
specially - it allows you to enter characters that are not on the keyboard. But sometimes you need the backslash so it can be unescaped with \\
. When displaying strings, python has both repr
and str
versions of the string. repr
gives you the literal sting version, while str
gives you the real string. Its a bit confusing that "literal" is literally not the string. If you escape the string and print it, you'll see the real characters.
>>> remove_string = '\\000'
>>> remove_string
'\\000'
>>> print(remove_string)
\000
You also used raw strings. Prepending with "r" tells python to stop using the backslash as a special string in a string literal. However, if you take the repr of that string later, you'll still get the special string literal represenation. No, problem though, because the string is correct.
>>> remove_string = r'\000'
>>> remove_string
'\\000'
>>> print(remove_string)
\000