A string can be identified by single quotation marks (...) or double quotation marks (& quot;... & quot. \ Can be used to escape quotation marks. strings can be identified by single quotation marks ('...') or double quotation marks. \ Can be used to escape quotation marks;
Scenario 1: If the string contains
Use single quotes instead of double quotes. Otherwise, use single quotes..
For example, to print:
Doesn 't:>>> "doesn' t"
"Yes", he said >>> '"yes", he said'
Alternatively, use the escape character \
Doesn 't:>>> 'doesn \ 'T'
"Yes", he said >>> "\" yes \ ", he said"
Case 2: What should I do if the character with \ above is treated as a special character? Add an r before the first quotation mark
>>>print 'c:\some\name'c:\someame
In this example, \ n is treated as a line break, and r is added before the first quotation mark:
>>>print r'c:\some\name'c:\some\name
Case 3: how to split a string into multiple rows ?? Use "..." "or '''...'''
>>> print 'apple\nbanana'applebanana>>> print '''applebanana'''applebanana>>> print """applebanana"""applebanana>>>
Scenario 4: how to connect multiple strings together ??
(1) usable + (variable + string text is also applicable)
>>> 'app' + 'le''apple'
(2) adjacent automatic connections (applicable to both strings)
>>> 'apple''love''banana''applelovebanana'>>>
Case 5: How can a string be intercepted (Retrieved )? Use subscript
>>> word = 'python'>>> word[0]'p'>>> word[1]'y'>>> word[-1]'n'>>> word[0:2]'py'>>> word[:2]+word[2:]'python'>>> len(word)6>>>
Scenario 6: to change a character in a string, what should I do ??
Word [1] = 'a' is not acceptable !!!
For more Python learning (II) --- articles related to strings, please follow the PHP Chinese network!