Simple notes for Python string attributes and common string methods

Source: Internet
Author: User
Tags string methods
Both single and double quotation marks can represent strings. The difference is when escaping.

If you are lazy to add an escape character, you can precede the string by adding R. For example:

Print R ' C:\some\name '

Do not wrap by adding a backslash to the string.

Print "" \  usage:thingy [OPTIONS]-  h      Display This Usage message-   h hostname    hostname to connect to< c5/> "" "

strings are connected by a plus sign and can be doubled by multiplication sign.
Strings can also be concatenated by writing them together, but not on variables:

' Py ' ' thon '

The string can be accessed like an array, and 0 represents the start character. In particular, 1 represents the last character, 2 is the 2nd character, and the result is obtained sequentially.

strings can be accessed by slicing. In particular, the use of negative numbers to slice.

s= "ABCDE" s[0]s[-1]s[-5]s[:-1] #去掉最后一个字符, such as line break +---+---+---+---+---+---+| P | y | T | H | o | N |+---+---+---+---+---+---+0 1 2 3 4 5 6-6-5-4-3-2-1

Slicing access out of bounds will get an empty set. No access control is required.

Cannot assign a value to a single character because the string is immutable. If you need a different string, then creat a new string, which can be easily achieved by using slices.
The built-in function Len returns the length of the string.

Use encode and decode to ask for string encoding and decoding. (For questions about the type of encoding, you need to open a file specifically to discuss)

Common methods:
StartsWith Detection Start
Example:

1. A = ' Leonis '
If A.startswith (' Le '):
print ' Le '
EndsWith Detection End
Example:

1. A = ' Leonis ' If A.endswith (' is '):p rint ' is '

In detects if it is part of
Example:

1. A = ' Leonis ' if ' o ' in A:print ' a '

The Find query contains
Example:

1. a= ' Leonis ' if A.find (' on ')! = -1:print (' on ')

Join connection string
Example:

1.

A = ' '
MyList = [' Brazil ', ' Russia ', ' India ', ' China ']print a.join (mylist)

2.

MyList = [' Brazil ', ' Russia ', ' India ', ' China ']print ' _ '. Join (MyList)

Split splitting string

1.

b = ' my '. Name: Is.. Leonis ' Print bprint b.split ('.. ')

2.

b = ' my '. Name: Is.. Leonis ' Print bprint b.split ('.. ', 1)
  • Related Article

    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.