Python string, list, tuple, collection, dictionary method

Source: Internet
Author: User
Tags iterable

Lists List

1, L.append (object), None

Add a single element at the end of the list, any type can, including lists or tuples, etc.

2, L.extend (iterable), None

Add multiple elements at the end of a list as a sequence

3, L.insert (Index, object), None

Add an element at the index position

4, L.clear (), None

Clears all elements of the list and becomes an empty list

5, l.copy () list

Get a copy of the list

6, L.count (A), integer

Returns the number of occurrences of a in the list

7, L.index (A, [Start, [stop]])

Returns a position in the first occurrence of a list where you can specify the start and end positions

8, L.pop ([index]), integer

Popup the corresponding position of the element, do not fill the parameters, the default popup last element

9, L.remove (A), None

Remove the first element A, the remaining element A is not good at

10, L.sort (Key=none, reverse=false), None

Sort the list by default in ascending order. If reverse=true, it is changed to descending order. You can pass a function to the key parameter, such as a lambda or a predefined one. Then follow this function to define what is the basis of the sort, for example, based on the last digit, or the following underlined number as the sort basis, etc.

This method will change the list sort

11, L.reverse (), None

Descending on a list

This method will change the list sort

Tuple tuples

1, T.count (A), integer

Returns the number of occurrences of a in the ancestor

2, T.index (A, [Start, [stop]]), integer

Return a where Yongzu first appears, you can specify the start and end ranges

Set set

1, S.add (element), None

Add an element to a collection

2, S.clear (), None

Clears all elements of a collection

3, S.copy (), set

Returns a copy of the original collection

4, S.remove (element), None

Removes an element from the collection, or an error if the element is not in the collection

5, S.discard (element), None

Ditto, but if the element does not have an error in the collection

6. Element S.pop ()

Randomly pops an element of the original collection

7, S.isdisjoint (S2), BOOL

Returns true if two sets do not have an intersection

8, S.issubset (S2), BOOL

Returns True if the S2 (sequence or collection) collection contains the S collection

9, S.issuperset (S2), BOOL

Returns True if the S collection contains a collection of S2 (sequences or collections)

10, S.difference_update (S2), None

s minus the intersection of S and S2 (sequence or set), returns no value,

This method will change the original set S

11, S. Intersection_update (S2), None

The intersection of S and S2 (sequences or sets), does not return any values,

This method will change the original set S

12, S.. Symmetric_difference_update (S2), None

The convergence of S and S2 minus the intersection of S and S2, no value is returned

This method will change the original set S

13, S.update (S2), None

S and S2 (sequences or sets), do not return any values,

This method will change the original set S

14, S.intersection (S2), set

Returns the intersection of S and S2 (sequence or set)

15, S.difference (S2), set

Returns the intersection of S and S2 (sequences or sets)

16, S. Symmetric_difference (S2), set

Returns the intersection of S and S2 minus S and S2, S2 can be sequences or collections

17, S.union (S2), set

Returns the set of S and S2 (sequences or sets)

Dictionary dict

1, D.clear (), None

Clears all key values in the dictionary

2, D.copy (), D

Returns a copy of a dictionary

3, D.pop (K[,d]), value

The value corresponding to the K key is ejected and the key value is removed. If the K key is not found but the value of D is set, the value of D is returned. If the D value is not set, then the K key is not found, then error.

4, D.popitem (), (K, V)

In the form of Ganso, a key value pops up (usually the first key value of a stack)

5, D.keys (), a Set-like object

All keys are returned in a similar list (actually returning objects that are more like a list of classes and do not handle duplicate values)

6, D.values (), a Set-like object

All values are returned in a similar list (actually returning objects that are more like a list of classes and do not handle duplicate values)

7, D.items (), a Set-like object

All key values are returned as a list, with each key value returned as a Ganso (actually returning an object that is more like a list of classes and does not handle duplicate values)

8, D.get (K[,d]), D[k] if k in D, else D.

If the dictionary exists k key then return the corresponding value, if not present, but fill in D value will return D value, otherwise error

9, D.setdefault (K[,d]), D.get (K,d), also set D[k]=d if K not in D

If the dictionary exists the K key returns the corresponding value, if it does not exist, but fills in the D value to establish the new key value in the original dictionary, and returns the value. However, if you do not fill in the D value, then error.

10, D.update (D2), None

D2 is also a dictionary. Merges the key values of the D2 into D, and if the same key exists, D2 overrides D

This method will change the original dictionary D

11, D.fromkeys (iterable, Value=none), Dict

This method is used to create a dictionary. All elements of an object that are iterated as keys, value as a unique value. Returns a dictionary of multiple-key pairs of single values (whether D is the same as null dictionary results)

string str

1, s.capitalize () str

To convert the initial letter to uppercase, it is important to note that if the first word is not capitalized, the original string is returned

2, S.upper () str    

Capitalize all the letters in the original string

3, S.lower () str

Lowercase all the letters in the original string (only a A-Z in the ASCII code)

4, S.casefold () str

Lowercase all the letters in the original string (to identify more objects to output bits lowercase)

5, s.swapcase () str

Swaps the letters in the original string that exist in the case

6, S.replace (old, new[, Count]) str

The replacement character. The Count parameter represents replacing several old characters. If you do not fill in the Count parameter, all old characters are replaced by default

7, S.expandtabs (tabsize=8), str

Replaces all tabs (\ t) in a string with spaces, with the default number of spaces replaced by 7 (8-1, where tabsize=0 means to remove \t,tabsize=1 or 2 for a space and the remainder to be n-1 spaces)

8, S.rjust (width[, Fillchar]) str

If the length of the original character is less than width, the remainder is padded with spaces on the left. If there is a fill character, the characters are used instead of spaces. (Note: only single-character)

9, S.ljust (width[, Fillchar]) str

Ditto. But it is filled on the right.

10, S.center (width[, Fillchar]) str

Ditto. But the left and right sides are also filled. The extra parts are on the right.

11, S.zfill (width), str

If the original character length is less than width, the remainder is padded to the left 0

12, S.find (sub[, start[, end]), int

Returns the position of the first occurrence of a substring in the original string, specifying the start and end positions. If the substring is not in the original string, return-1 Note: (Start,end]

13, S.index (sub[, start[, end]), int

Ditto, but if the substring is not in the original string, the error Note: (Start,end]

14, S.rindex (sub[, start[, end]), int

Same as index, but from right to left of string, but returns the leftmost first character position of a substring.

15, S.rfind (sub[, start[, end]), int

Same as find, but from right to left of the string, but returns the leftmost first character position of a substring

16, S.split (Sep=none, maxsplit=-1), List of strings

Returns a list with the Sep as a delimiter. Maxsplit delegates are separated several times, default to full delimited

17, S.rsplit (Sep=none, maxsplit=-1), List of strings

Ditto. But from right to left

18, S.splitlines ([keepends]), List of strings

Returns a list of characters that are separated by a newline character as delimiters. The default keepends is False, which indicates the resulting list, and the elements of the list are stripped of line breaks. Keep line breaks If you change to True

19, S.partition (Sep), (Head, Sep, tail)

This method is used to split the string according to the specified delimiter. Returns a tuple of $3 If the string contains the specified delimiter. The first is a substring to the left of the delimiter, the second is the delimiter itself, and the third is the substring to the right of the delimiter.

If the specified delimiter is not included, the first is the original string and the 23rd is a null character

20, S.rpartition (Sep), (Head, Sep, tail)

Ditto, but from right to left, and if the specified delimiter is not included, the 12th is a null character and the second is the original string

21, strip ([chars]), str

The default is to return a string that removes whitespace from the left and right sides. If the argument has a sub-character, all of the left and right sub-characters are removed

22, S.rstrip ([chars]), str

Ditto, but only the right character is removed.

23, S.lstrip ([chars]), str

Ditto, but only the left character is removed

24, S.startswith (prefix[, start[, end]), BOOL

Determines whether the string begins with a string, or true if it is. You can specify the start and end positions

25, S.endswith (suffix[, start[, end]), BOOL

Ditto, but judging by the end

26, S.count (sub[, start[, end]), int

Returns the number of occurrences of a substring in the original string. You can specify the start and end positions

27, S.join (iterable), str

Fills the original character between the elements of the sequence

28, S.encode (encoding= ' utf-8 ', errors= ' strict '), bytes

encoding, errors parameters can be selected a lot, including ' Ignore '

29, S.isidentifier (), BOOL

Whether it is a python keyword, etc., if true

30, S.isalnum (), BOOL

Whether the string is all composed of numbers, English or Chinese characters (including Roman numerals, etc.), if true

31, S.isdecimal (), BOOL

Whether the string contains only 10 binary digits

True:unicode digits, full-width digits (double-byte)

False: Roman numerals, Chinese numerals

Error:byte number (single byte)

32, S.isnumeric (), BOOL

Whether the string contains only numbers

True:unicode numbers, full-width numerals (double-byte), Roman numerals, Chinese numerals

False: None

Error:byte number (single byte)

33, S.isdigit (), BOOL

Whether the string contains only numbers

True:unicode number, byte digit (single byte), full-width digit (double byte), Roman numerals

False: Chinese numerals

Error: None

34, S.isspace (), BOOL

Whether the string contains only spaces (spaces, tabs, line breaks), if true

35, S.isalpha (), BOOL

Whether the string contains only letters, if true

36, S.islower (), BOOL

Whether all letters in the string are lowercase (can contain non-alphabetic characters), if True

37, S.isupper (), BOOL

Returns true if the letters in the original string (which can contain other content, such as numbers) are all uppercase.

38, Isprintable (), BOOL

Whether all characters in the string are visible (for example \ n is not visible), if True

39, S.istitle (), BOOL

Whether the first letter of each word in the character is capitalized (except for the letters, only spaces and normal punctuation are allowed), if true

40, S. Maketrans (x, Y=none, Z=none), Dict

41, S.translate (table), str

Refer to the above statement (table here refers to a dictionary mapping table)

42, S.format_map (mapping), str

Note: The key cannot be a purely numeric

43, S.title () str

the first letter of each word in the character is capitalized (allows various characters to be cut off in the middle)

Python string, list, tuple, collection, dictionary method

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.