Python learning (2) --- string

Source: Internet
Author: User
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.