Single, double, and triple quotation marks in Python
編輯:Python
There are single quotation marks and double quotation marks in the string
>>> "it's a string"
"it's a string"
There are double quotation marks and single quotation marks in the string
>>> 'it is "python"'
'it is "python"'
String has multiple lines with three quotation marks
>>> s='''it's a string '''
>>> s
"it's\na\nstring\n"
>>> print(s)
it's
a
string
If you use single or double quotation marks, you will get an error , Also note the newline character in the string .
Single quotation marks 、 Double quotation marks can also represent multiline strings , But you need to put a backslash after each line
>>> s='it\
is\
a\
strng\
'
>>> s
'itisastrng'
Escape backslash Such as : The string represented by single quotation marks has single quotation marks
>>> 'it\'s a string'
"it's a string"
other ( Ignore the following is Different colors )
>>> s='It\nis\tpython'
>>> s
'It\nis\tpython'
>>> print(s)
It
is python
Original string r Output whatever it is , You can find that the third line escapes the newline character into ordinary characters with the escape character
>>> s=r"this\nis\na\nstring"
>>> s
'this\\nis\\na\\nstring'
>>> print(s)
this\nis\na\nstring