Python itself uses \ to escape some special characters, such as when adding quotation marks in a string
s ='i\'m superman'print(s) # i'm superman
In order to prevent the conflict with the quotation mark of the string itself, use \ to escape. Normally, this will not cause any problems, but when you want to use \ to escape , it is more confusing, for example, we want to output One , you have to write two , otherwise it will report a syntax error, because \ escapes the quotation mark after it, you must use .
# Wrong way
# print '\'
# Correct writing
print('\') # \
# Native string
print(r'\') # \
Escape the \ so that it does not have the escape function, so that it can be output correctly. When using a native string, the output shows two \s, which looks like writing a few and outputting a few, if you think so , You can try to see if you can output an odd number of .
Knowledge point expansion:
How to output backslashes \ as a string separately in python
The situation is like this, there is a file named'\u5feb\u901f\u4e0a\u4f20'
Then when I use os.listdir('.') to display, it automatically escapes the backslash, which becomes'\u5feb\u901f\u4e0a\u4f20'
So the question is, how to replace double slashes with single slashes?
I tried to replace the string, but a.replace('\','') will cause an error because the backslash escapes the single quotation mark and cannot find the end of the string. Use a.replace(r' \', r'') will not work either.
Is there any good way to help me achieve my goal?
name ='\u5feb\u901f\u4e0a\u4f20'
print name
\ u5feb\u901f\u4e0a\u4f20
print repr(name)'\u5feb\u901f\u4e0a\u4f20'
print [name]['\u5feb\u901f\u4e0a\u4f20']
print [name][0]
\ u5feb\u901f\u4e0a\u4f20
So far, this article on how to output backslashes in python is introduced. For more information about how to output backslashes in python, please search for ZaLou.Cn's previous articles or continue to browse related articles below. Hope you will support ZaLou more in the future. .Cn!
Recommended Posts