May 10, 2021 Python2
1. Single double quote strings are the same
>>> 'abc',"abc"
('abc', 'abc')
>>>
When your python follows the example above, this time the single double quote string is the same. The two are interchangeable.
We can even introduce each other directly in single double quotes
>>> 'abc"d',"abc'd"
('abc"d', "abc'd")
In any expression, python automatically merges connected strings, even though there is no connection between them
>>> 'abc"d',"abc'd"
('abc"d', "abc'd")
Also, as in the above examples, a comma is added to the middle of the string, resulting in a metagroup instead of a string
2. Use escape characters to represent special characters
>>> a='abc\ndef\r'
>>> a
'abc\ndef\r'
>>> print(a)
abc
def
>>>
Look at the example above, if we print all the characters directly with variables, if we print directly with variables, the escape characters will change from the original string to binary values, for example, if we print directly with variables, and so on
Common escape characters:
Escape | Significance |
---|---|
\ | continuous, followed by the above line |
\ | Backslash |
\' | Single quotes |
\" | Double quotes |
\a | Bell |
\b | Retrogression |
\f | Change the page |
\n | Line change |
\r | Return |
\t | Horizontal tabs |
\v | Vertical tabs |
\N{id} | Unicode database id |
\uhhhh | The heige value of unicode 16 bits |
\Uhhhh | Heige value of unicode32 bits |
\xhh | He heteed value |
\ooo | Octal values |
\0 | Null |
\other | Not escaped |
3.raw String suppression escape
There are many times when we need to open a file, etc., then we need to enter a path, especially the window path, mostly using a backslash, this time there will be a problem
For example:
handler=open('c:\nb123.txt','w')
This time there was a problem, originally opened nb123 this text file, but because there is a backslash in front, in the escape is a new line, causing ambiguity
So, we need to change the path to the following:
handler=open(r'c:\nb123.txt','w')
handler=open('c:\\nb123.txt','w')
This time it is legal to use r to suppress escape, or to use a double backslash
4. Use triple double quotes to enter multiple strings
>>> a="""aaabbb
ccdcdef'ddfdfd"""
>>> a
"aaabbb\nccdcdef'ddfdfd"
It is often used for document strings, or to comment on large pieces of code