Python learning (2)---Strings

高洛峰
Release: 2017-02-13 17:48:33
Original
1264 people have browsed it

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
Copy after login



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
Copy after login

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>>>
Copy after login

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

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


>>> 'app' + 'le''apple'
Copy after login

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


>>> 'apple''love''banana''applelovebanana'>>>
Copy after login

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
>>>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template