Python String Type

Source: Internet
Author: User

First, define a string

in [+ ' This isa string '        # You can use single quotation mark/double quotation mark/three quotation mark to define string type  in []: str = " This was a string "        # in Python, there is no difference between single and double quotes in  [] ' This isa string ' 
    # Three quotes can be written as "' or" ", and three quotes can be used to make annotations in addition to string types 
In ["hello\nworld"    # Single and double quotes if you want to add \ n as a newline character in the cross-line definition    - """ Hello          # Three quotes can be entered    directly on the next line .....: World" " "

Second, the index and slice of the string

in [ -]: a ='ABCDE'In [ -]: a[0] # The index value starts at 0 and represents the first character in a string out[ -]:'a'In [ -]: a[1] # takes the second character in a string out[ -]:'b'In [ in]: a[-1]#-1 means a countdown character, -2 represents the second-to-last character, and so on out[ in]:'e'In [ -]: a[0:2] # represents the first to third (not including the third) character of this range out[ -]:'AB'In [ to]: a[:2] # Zero can be omitted if the ellipsis means starting from the first index fetch out[ to]:'AB'In [ +]: a[1:] # This writes a second fetch from the last index out[ +]:'BCDE'In [ -]: a[-4:-2] # indicates the character out[the range from the fourth to the penultimate (excluding the penultimate one).  -]:'BC'In [ the]: a[::1] # indicates that every other number is taken once out[ the]:'ABCDE'In [ *]: a[::2] # indicates that every two numbers are taken once out[ *]:'Ace'

Python String Type

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.