1. Single-and double-argument strings are the same
When your Python writes according to the example above, this time the single-and-double-argument string is the same. The two can be interchangeable.
We can even introduce each other directly in a single-pair
>>> ' abc ' d ', "ABC ' d" (' abc ' d ', "abc ' d")
In a casual expression, Python will voluntarily merge the concatenated strings, although they are not connected by +
>>> ' abc ' d ', "ABC ' d" (' abc ' d ', "abc ' d")
Also, just like the examples above, add commas to the middle of the string. The final form is a tuple, not a string.
2. Using escape characters to represent special characters
Look at the example above, with the escape character between the strings, assuming that we print directly with the variable, he will print all the characters, but suppose print is used. The escape character becomes a binary value from the original string, such as: \ n for tabs, and so on
Common escape characters:
Escape |
Significance |
\ |
Continuous. Then the top line |
\\ |
Back slash |
\‘ |
Single cited |
\" |
Double cited |
\a |
Bell |
\b |
Retrogression |
\f |
Page change |
\ n |
Line break |
\ r |
Return |
\ t |
Horizontal tab |
\v |
Vertical tab |
\n{id} |
Unicode database ID |
\uhhhh |
Hexadecimal value of the UNICODE16 bit |
\uhhhh |
Hexadecimal value of the UNICODE32 bit |
\xhh |
Hexadecimal value |
\ooo |
Eight-binary value |
/ |
Null |
\other |
Do not escape |
3.raw string Suppression Escape
There are very many times when we need to open files and so on, then we need to enter the path. Especially the path of window. Most use backslashes, this time there will be a failure
Like what:
Handler=open (' C:\nb123.txt ', ' W ')
There's a problem at this point. Originally opened nb123 This text file, but because there is a backslash in front, in escaping it is a newline, causing ambiguity
So we need to change the path to look like this:
Handler=open (R ' C:\nb123.txt ', ' W ') handler=open (' C:\\nb123.txt ', ' W ')
This time is legal, use R to suppress escape. Or use a double-backslash
4. Use triple-double-quotes to enter multiple strings
>>> a= "" "Aaabbbccdcdef ' Ddfdfd" "" >>> A "Aaabbb\nccdcdef ' DDFDFD"
It is often used for document strings, or to stare at large sections of code
Here it is. Thank you
------------------------------------------------------------------
Click the jump 0 Basic python-folder
0 Fundamentals python-7.2 String Constants