How does python output backslashes

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

How does python output backslashes
How does Python output integers
How does python update packages
How does python perform matrix operations
How does Python list update value
How does python call java classes
How does python import dependency packages
How does python enter interactive mode
How does Python generate xml files
How does python determine prime numbers
How does python improve the calculation speed
How does python call its own function
How does Python handle the json module
How does python distinguish return and yield
How does Python call cmd using OS modules
How does Python store data to json files
How python was invented
How Python parses XML
How does python call the key of a dictionary
How does python get input examples from the keyboard
How does python handle the program cannot be opened
How does python judge the module installation is complete
How to comment python code
How Python converts string case
How long does it take to learn python by myself?
How to learn python quickly
How to uninstall python plugin
Python output mathematical symbols example
Does Python support multiple inheritance?
How Python implements FTP function
How to understand python objects
How to use python tuples
Explain how python references package package
Python string three formatted output
python how to view webpage code
How to use python thread pool
How to write python configuration file
How to wrap in python code
What does rc1 mean in python
How to save the python program
What does def in python do
How to omit parentheses in Python
How to install Python 3.8 on CentOS 8
How to install Python 3.8 on Ubuntu 18.04
How to write classes in python
How to filter numbers in python
How to read Excel in Python
How to install Python on CentOS 8
How to solve python dict garbled
How to view errors in python
What does np do in python
How to write return in python
How Python operates on file directories
How Python implements the timer function
How to view the python module
How to understand variables in Python
How to clear variables in python
How to understand python object-oriented programming
Python realizes console output color font
How to use SQLite in Python