27 Common operations for Python strings

Source: Internet
Author: User

There are string mystr = "Hello World itcast and Itcastcpp", the following are common operations:

1. Mystr.find (str, start=0, End=len (mystr))

Detects if STR is contained in MYSTR, or returns 1 if it is the index value that returns the start.

  

2. Mystr.index (str, start=0, End=len (mystr))

Just like the Find () method, only if STR does not report an exception in MyStr.

  

Use the Find to fetch file suffix:

·

3. Mystr.count (str, start=0, End=len (mystr))

Returns the number of times that STR appears in the mystr between star and end

  

4. mystr.replace (str1, str2, mystr.count (str1))

Replace the str1 in the mystr with the str2, if Count is specified, the replacement is not more than count times.

Note that the replaced string is not assigned to MYSTR

5. mystr.split (str, maxsplit)

Slice mystr with the Str delimiter, and if Maxsplit has a specified value, only maxsplit strings are delimited.

6. Mystr.capitalize ()

Capitalize the first character of a string

7. Mystr.title ()

Capitalize the first letter of each word in a string

  

8. Mystr.startswith (obj)

Checks whether the string starts with obj, or returns True, otherwise false.

9. Mystr.endswith (obj)

Checks whether the string ends with obj, or returns True, otherwise false.

  

Mystr.lower ()

  Converts all uppercase characters in a string to lowercase.

Mystr.upper ()

 Converts all uppercase characters in a string to lowercase.

----can be used to determine the user's exit input characters.

Mystr.ljust (width)

  Returns the left alignment of an original string and fills the new string with the width of length with a space.

Mystr.rjust (width)

 Returns an original string that is right-aligned and fills a new string of length width with a space.

----Remember the print () left-aligned display and right-aligned display.

Mystr.center (width)

  Returns the center of the original string and fills the new string with a space of length width.

Mystr.lstrip ()

  Removes the white space character from the left side of the mystr string.

Mystr.rstrip ()

 Removes the white space character at the end of the mystr string.

Mystr.strip ()

Removes whitespace characters at both ends of the mystr string.

·

----Gets the processing of the string when the user enters data.

Mystr.rfind ()

 Similar to find (), it's just looking from the right.

Mystr.rindex ()

  Similar to index (), but starting from the right.

Mystr.partition (str)

 Divide the mystr into three parts, before STR, str and STR.

Mystr.rpartition (str)

 Similar to partition (), but starting from the right.

  

Mystr.splitlines ()

  Returns a list containing the rows as elements, separated by rows.

Read----file

Mystr.isalpha ()

 Returns true if all mystr characters are letters, otherwise false.

Mystr.isdigit ()

  Returns True if the mystr contains only numbers, otherwise false is returned.

Mystr.isalnum ()

  Returns true if all characters in the mystr are letters or numbers, otherwise false.

Mystr.isspace ()

 Returns True if the mystr contains only spaces, otherwise false is returned.

Str.join (MYSTR)

After each character in the mystr is inserted into STR, a new string is constructed. A list of links.

  

An interview question: Given a string astr, returns the second-to-last substring with a space or "\ T" split.

27 Common operations for Python strings

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.