Strings can be ‘‘ expressed in or "" enclosed. What if the string itself contains ‘ ? For example, if we want to represent I‘m OK a string, we can say it in " " parentheses:
"I ' m OK"
Similarly, if a string is contained " , we can use it as a ‘ ‘ means of enclosing:
' Learn ' Python "in Imooc '
What if the string contains ‘ and contains both " ?
At this point, you need to "escape" some special characters of the string , and the Python string is \ escaped.
To represent a stringBob said "I‘m OK".
Because ' and ' can cause ambiguity, so we insert a representation before it that \ this is an ordinary character and does not represent the beginning of the string, so the string can be represented as
' Bob said \ ' i\ ' m ok\ '. '
Note: the escape character \ does not count toward the contents of the string.
The usual escape characters are:
\ n indicates a newline \ t means a tab \ \ means \ character itself
The following two lines of text are printed in Python's string representation:
Python is started in 1989 by "Guido".
Python is free and easy to learn.
Reference:
s = ' Python was started in 1989 by \ ' Guido\ '. \npython are free and easy to learn. ' Print S
Defining strings in Python