Python Learning (ii)---string

Source: Internet
Author: User
Strings can be identified by single quotation marks (' ... ') or double quotation marks ("..."). \ can be used to escape quotation marks;

Scenario One: If the string single quotes without double quotes, quoted as double quotes, otherwise quoted in single quotation marks。

For example, to print:

Doesn ' t: >>> "doesn ' t"

"Yes", he said >>> ' yes ', he said '

Alternatively, use the escape symbol \

Doesn ' t: >>> ' doesn\ ' t '

"Yes", he said >>> "\" yes\ ", he said"



Scenario Two: What happens if the character preceded by \ is treated as a special character? Add an R before the first quotation mark

>>>print ' C:\some\name ' c:\someame

In the example, \ n is considered a newline symbol, then the first quotation mark is preceded by R:

>>>print R ' C:\some\name ' C:\some\name


Scenario Three: The string wants to be divided into multiple lines, how to do?? Use "" "..." "" or ' ... '

>>> print ' Apple\nbanana ' applebanana>>> print ' Applebanana ' applebanana>>> print ' "" Applebanana "" "Applebanana>>>


Scenario four: How to concatenate multiple strings together??
(1) can use + (variable + string text also applies)

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

(2) Adjacent automatic connection (for two all string literals)

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


Scenario Five: How does 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 Six: Want to change a character in a string, what to do??
WORD[1] = ' A ' This is not going to work!!!


Learn more about Python (ii)---string related articles please follow topic.alibabacloud.com!

  • 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.