A summary of the methods of Python sequences _python

Source: Internet
Author: User

Recently in the Python project, I specifically sorted out the Python sequence method. Sequence sequence is the most basic data structure in Python, this article first makes a simple generalization of the sequence, and then simply explains how all the sequences can be used in general.

Any sequence can refer to its elements (item).

The following built-in function (built-in function) is available for lists (tables, fixed-value tables, strings)

#s为一个序列

Len (s) Returns: number of elements contained in a sequence
Min (s) Returns: the smallest element in a sequence
Max (s) Returns: the largest element in a sequence
All (s) Back: True, if all elements are true
Any (s) Return: True if either element is True

The following method primarily functions as a query and does not alter the sequence itself, which can be used for tables and table values:

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

SUM (s) Returns: The and of all elements in a sequence
S.count (x) Back: X number of occurrences in s
S.index (x) Back: x subscript for the first time in S

The following methods apply only to tables because the elements of a fixed-value table cannot be changed:

#l为一个表, L2 for another table


L.extend (L2) Add all elements of table L2 at the end of table L
L.append (x) Attach an X element at the end of L
L.sort () Sort the elements in L
L.reverse () Reverse the elements in L
L.pop () Return: The last element of table L, and delete the element in table L
Del L[i] Delete the element

(These methods are all done on the original table, and will have an effect on the original table instead of returning a new table)

The following are some of the methods used for strings. Although strings are a special form of a fixed-value table, there are some methods of string (string) classes that change strings. 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 does not contradict the characteristics of the fixed-value table.

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

Str.count (sub) Returns: The number of times a sub appears in Str
Str.find (sub) Back: Starts from the left and looks for the first occurrence of a sub in Str. If Str does not contain a sub, return-1
Str.index (sub) Back: Starts from the left and looks for the first occurrence of a sub in Str. If Str does not contain a sub, cite an error
Str.rfind (sub) Returns: Starts from the right, looking for the first occurrence of a sub in Str. If Str does not contain a sub, return-1
Str.rindex (sub) Returns: Starts from the right, looking for the first occurrence of a sub in Str. If Str does not contain a sub, cite an error
Str.isalnum () Return: True If all characters are letters or numbers
Str.isalpha () Return: True If all characters are letters
Str.isdigit () Return: True If all characters are numeric
Str.istitle () Return: True If all the first letters of a word are uppercase
Str.isspace () Return: True If all characters are spaces
Str.islower () Return: True If all characters are lowercase letters
Str.isupper () Return: True If all characters are uppercase
Str.split ([Sep,[max]]) Back: Starts from the left, takes a space delimiter (separator), divides str into multiple substrings, and divides the max times altogether. The resulting substring is returned in a table. A comma or other delimiter can be used Str.split (', ')
Str.rsplit ([Sep,[max]]) Back: Starts from the right, divides the STR into multiple substrings with a space delimiter (separator), and divides the max times altogether. The resulting substring is returned in a table. A comma or other delimiter can be used Str.rsplit (', ')
Str.capitalize () Back: Capitalize the first letter of STR
Str.lower () Back: Change str all letters to lowercase
Str.upper () Back: Change str all letters to uppercase
Str.swapcase () return: Change str capital letter to lowercase, 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 with a width of length, placing the original string in the center of the string, and other spaces in the space.
Str.ljust (width) Returns a string that has a width of length, puts the original string left-aligned into the string, and the other free spaces.
Str.rjust (width) Returns a string with a width of length, where the original string is right-aligned and placed in the string, and other spaces are blank.
Str.join (s) Returns: Merges the elements of S, with Str as a delimiter, into a string.
Str.strip ([Sub]) Returns: Removes the space at the beginning and end of the string. You can also supply the argument sub, removing the sub at the beginning and end of the string
Str.replace (Sub, new_sub) Back: Replaces the sub in Str with a new string new_sub

Thank you for reading, I hope to help you, thank you for your support for this site!

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.