Home  >  Article  >  Backend Development  >  Python learning (2)---Strings

Python learning (2)---Strings

高洛峰
高洛峰Original
2017-02-13 17:48:331212browse

Strings can be identified by single quotes ('...') or double quotes ("..."). \ can be used to escape quotes;

Case 1: If in the string has only single quotes but no double quotes, use double quotes, otherwise use single quotes Quote in quotation marks.

                                                                                                       "yes",he said' ",he said >>>"\"yes\",he said "

Case 2: The character

with \ in front of

is regarded as What to do with special characters?

Add an r

>>>print 'c:\some\name'c:\some
ame



before the first quotation mark. In the example \n is treated as a newline symbol, then Add r before the first quotation mark:

>>>print r'c:\some\name'c:\some\name

Scenario 3: What should I do if I want to split the string into multiple lines? Use "" "...""" or '''...'''

>>> print 'apple\nbanana'apple
banana>>> print '''apple
banana'''apple
banana>>> print """apple
banana"""apple
banana>>>

Case 4: How to concatenate multiple strings in Together??

(1) You can use + (variable + string text is also applicable)


>>> 'app' + 'le''apple'

(2) Adjacent ones are automatically connected together (Applicable when both are string literals)


>>> 'apple''love''banana''applelovebanana'>>>

Case 5: How is the string 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: I want to change a character in the string, what should I do?

Word[1] = 'a' This doesn't work!! !


For more Python learning (2)---Strings, please pay attention to the PHP Chinese website for related articles!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn