Python Quick Tutorial (supplement): Python's Sequence method

Source: Internet
Author: User

Any sequence can refer to its element (item).

The following built-in functions (built-in function) can be used for sequences (tables, fixed-value tables, strings):

# s for a sequence

Len (s) returns: the number of elements contained in the sequence

MIN (s) returns: the smallest element in a sequence

Max (s) returns: The largest element in a sequence

All (s) returns: True if all elements are true

Any (s) returns: True if either element is True

The following method primarily functions as a query, does not change the sequence itself, can be used for tables and fixed value tables:

SUM (s) returns: All elements in the sequence and

# x is the element value, I is the subscript (the position of the element in the sequence)

S.count (x) returns: X number of occurrences in s

S.index (x) returns: x subscript for the first occurrence in S

Because the elements of the fixed value table are immutable, the following method applies only to tables:

# L is a table, L2 is another table

L.extend (L2) Add all elements of table L2 at the end of table L

L.append (x) append x element at the end of L

L.sort () sort the elements in L

L.reverse () reverse the elements in L

L.pop () returns: the last element of table L, and deletes the element in table L

Del L[i] Delete the element

(All of these methods are performed on the original table, and will have an effect on the original table instead of returning a new table.) )

Here are some methods for strings. Although a string is a special kind of a fixed value table, there are some methods of string class that change the string. The essence of these methods is not to manipulate the original string, but to delete the original string, and then create a new string, so it is not inconsistent with the characteristics of the fixed value table.

#str为一个字符串, a sub is a substring of str. S is a sequence whose elements are strings. Width is an integer that describes the width of the newly generated string.

Str.count (sub) Return: Number of sub occurrences in Str

Str.find (sub) return: From the left, find the position of the sub in Str for the first time. If Str does not contain a sub, returns-1

Str.index (sub) return: From the left, find the position of the sub in Str for the first time. If the STR does not contain a sub, cite the error

Str.rfind (sub) return: From the right, find the position where the sub first appears in Str. If Str does not contain a sub, returns-1

Str.rindex (sub) return: From the right, find the position where the sub first appears in Str. If the STR does not contain a sub, cite the error

Str.isalnum () returns: True If all characters are letters or numbers

Str.isalpha () returns: True If all characters are letters

Str.isdigit () returns: True If all characters are numeric

Str.istitle () returns: True if the first letter of all words is uppercase

Str.isspace () returns: True if all characters are spaces

Str.islower () returns: True if all characters are lowercase letters

Str.isupper () returns: True if all characters are uppercase

Str.split ([Sep, [MAX]]) returns: Starting from the left, dividing str into substrings with a space divider (separator), dividing the total number of Max times. Returns the resulting substring in a table. You can Str.split (', ') by using commas or other delimiters

Str.rsplit ([Sep, [MAX]]) returns: Starts from the right, divides str into substrings with a space divider (separator), and splits the max times in total. Returns the resulting substring in a table. You can Str.rsplit (', ') by using commas or other delimiters

Str.join (s) returns: The elements in S, with Str as the delimiter, are merged into a string.

Str.strip ([Sub]) returns: Removes whitespace at the beginning and end of the string. You can also provide a parameter sub, removing the sub at the beginning and end of the string

Str.replace (Sub, new_sub) return: Replace sub in Str with a new string new_sub

Str.capitalize () Return: Capitalize the first letter of STR

Str.lower () Return: Change all str letters to lowercase

Str.upper () Return: Change all str letters to uppercase

Str.swapcase () return: Change str uppercase to lowercase and lowercase to uppercase

Str.title () Return: Capitalize the first letter of each word of str (separated by a space)

Str.center (width) returns: A string of width length, placing the original string in the center of the string, and other vacant spaces.

Str.ljust (width) returns: Width-length string, left-aligned the original string into the string, and other vacant spaces.

Str.rjust (width) returns: Width-length string, right-aligned the original string into the string, and other vacant spaces.

All-in-one programmer Exchange QQ Group 290551701, gather a lot of Internet elite, technical director, architect, Project Manager! Open source technology research, Welcome to the industry, Daniel and beginners are interested in engaging in IT industry personnel to enter!

Python Quick Tutorial (supplement): Python's Sequence method

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.