python study notes string escaping

The escape character is \ followed by single quotes or double quotes, which allows the result to directly output single quotes or double quotes. This is a specific interpretation of the Python compiler and is also widely used in various other languages. For example, if there is a \" in the string, if there is no \, it will be considered as a quotation mark. If an error is reported, if \" is added, it will be considered as a character and output as it is.

Here are some commonly used escape characters

1,\n newline character

In a python string, the compiler will perform a newline operation when it encounters \n.
As shown in the figure below, you can see that because there is \n in the string, a newline operation is performed, so the output result has two lines.

Insert image description here
If we are in \n Adding a \ in front of it can cancel the special meaning of the escape character, as shown in the figure below
Insert image description here

2,\tTab tab character

\t is also a commonly used escape character in Python. Its special meaning is the same as the Tab creation function, which is equivalent to pressing a Tab key or four spaces.

The effect is as shown in the figure below:
Insert image description here
In the same way, to cancel the special meaning, just add a \ in front. It is the same as above, so I won’t go into details here.

3. r and R unescaping the entire string

If there are multiple escape characters in a string, it would be too troublesome to add \ in front of each one. Here is a simple and quick way, which is to add r or R in front of the string.

Results as shown below:

Insert image description here
Of course, python has many other escape characters, but they are not commonly used. Interested friends can go to Baidu to check them out.

Guess you like

Origin blog.csdn.net/will__be/article/details/103440819