Learning Summary of String functions in Python3.2 _python

Source: Internet
Author: User
Tags alphabetic character numeric iterable string methods alphanumeric characters

Sequence Types

There are six types of sequence: strings, byte sequences (bytes objects), byte arrays (ByteArray objects), list, tuple, Range objects.

Common operations supported by sequence types:
Member check: in, not in
Connection: +
Copy: *
Subscript value: S[i]
Slices: s[i:j]
Length check: Len (s)
Min Value: min (s)
Max value: Max (s)
Index value: S.index (i)
String statistics: S.count (i)

String Methods

A method of judging a class, typically returning a Boolean value:

Str.endswith (suffix[, start[, end]]):

Returns TRUE or FALSE to determine whether the string ends with the specified suffix. Start and end specify the starting range of the judgment, the default full string. Such as:

Copy Code code as follows:

' ABCDE '. EndsWith (' de ')-->true
' ABCDE '. EndsWith (' De ', 0, 3)-->flase


Str.startwith (prefix[, start[, end]]):

In contrast to Str.endwith (), determines whether the string starts with the specified prefix.

Str.islower ():

Determines whether alphabetic characters in a string are all lowercase, and this method only judges alphabetic characters in the string, ignoring other characters. The string must contain at least one alphabetic character, otherwise it returns false. Such as:

Copy Code code as follows:

' China '. Islower ()-->false
' Ab China '. Islower ()-->true

Str.isupper ():

In contrast to the St.islower () method, determine whether all alphabetic characters are all uppercase.

Str.istitle ():

Determines whether the first letter of each word in a string is capitalized. The string must contain at least one alphabetic character, otherwise it returns false. Even if the initials are preceded by non-alphanumeric characters, such as Chinese, numerals, underscores, and so on, they do not affect the judgment of the first letter character.

Copy Code code as follows:

' China '. Istitle ()-->false//string does not contain letters, returns FALSE
' Chinese abc '. Istitle ()-->true//Although the alpha character A is preceded by a non-alphanumeric character, it still returns True
'-abc xyz '. Istitle ()-->false//The first letter of the second word is not uppercase and returns False

Str.isalnum ():

Determines whether the string contains only literal numeric characters, and the string contains only the Chinese text-conforming method. Returns False if the string contains spaces, underscores, ~, and other non-numeric characters. Such as:

Copy Code code as follows:

' 3 '. Isalnum ()-->true
' China '. Isalnum ()-->true
'-'. Isalnum ()-->false

Note: Alphanumberic is a special term that indicates that the string consists of numeric characters or literal characters. For example, ' 3 ' includes a numeric character, ' A ' contains a literal character, while ' 3a ' includes both numeric and alphabetic characters.

Str.isalpha ():
Determines whether the string contains only literal characters, and the string contains only the Chinese text-conforming method. Such as:

Copy Code code as follows:

' China '. Isalpha ()-->true
' 3 '. Isalpha ()-->false

str.isidentifier ():

To determine whether the string is a valid identifier, the string contains only the literal compliance method, and it is actually judged whether the variable name is legitimate. Such as:

Copy Code code as follows:

' _a '. Isidentifier ()-->true
' 3a '. Isidentifier ()-->false
' China '. Isidentifier ()-->true

Str.isprintable ():

Determines whether the characters contained in a string are all printable. The string contains nonprinting characters, such as escape characters, and returns false.

Str.isspace ():

Determines whether a string contains only spaces or tabs. Note: Whitespace characters are different from whitespace, such as:

Copy Code code as follows:

'. Isspace ()-->false
'. Isspace ()-->true

Str.isdecimal ():

Determines whether a string contains only decimal digit characters, including the decimal digit character representation of a multinational language. Such as:

Copy Code code as follows:
' 3 '. Isdecimal ()-->true
' \u0660 '. Isdeciaml ()-->true

Decimal digit form Reference for other languages: http://www.fileformat.info/info/unicode/category/Nd/list.htm

Str.isdigit ():

Determines whether a string contains only numbers, and the numbers here include decimal digits and other special numbers (such as superscript digits). Generally, a number is a character with the following attribute value: Numeric_type=digit or Numeric_type=decimal.

Str.isnumeric ():

Determines whether a string contains only numeric characters. The range of numeric characters is large, in general, numeric characters are characters that have the following attribute values: Numeric_type=digit, Numeric_type=decimal, or numeric_type=numeric.
Comparison of Isdecimal (), IsDigit (), IsNumeric (), and several methods to detect the extent of expansion in turn.


format the class method, returning a new formatted string:

Str.encode (encoding= "Utf-8", errors= "strict"):

Encodes a string in utf-8 format.

Str.lower ():

Convert all alphabetic characters to lowercase without having to take care of other non-alphanumeric characters. It is also legal to string all non-alphanumeric characters, but returns the original string. Such as:

Copy Code code as follows:

' China 123ABC '. Lower ()--> ' China 123abc '
' China 123 '. Lower ()--> ' China 123 '//No error, return original string

Str.upper ():

Instead of Str.lower (), convert all alphabetic characters to uppercase. Such as:

Copy Code code as follows:

' China 123abc '. Upper ()--> ' China 123ABC '
' China 123 '. Upper ()--> ' China 123 '

Str.swapcase ():

Swap uppercase and lowercase letters in a string, and capitalize to lower case, lowercase to uppercase. Non-alphabetic characters are not to be regulated. Such as:

Copy Code code as follows:

' China 123Ab '. Swapcase ()--> ' China 123aB '
' China 123 '. Swapcase ()--> ' China 123 '//No error, return original string

str.capitalize ():

Capitalize the first letter of the string, and the rest lowercase. If the first character of the string is not an alphabetic character, the original string is returned. The string contains only non-alphabetic word compliance, but returns the original string. Such as:

Copy Code code as follows:

' AB CD '. Capitalize ()--> ' AB CD '//convert only the first letter of the string
' China ab 123CD '. Capitalize ()--> ' China ab 123CD '/The first character is not an alphabetic character, return the original string
' China 123 '. Capitalize ()--> ' China 123 '//No error, return original string

Str.title ():

The first letter of each word in the string is capitalized and the remainder is lowercase. The first character of the word is not an alphabetic character and does not affect the conversion. The string contains only non-alphabetic word compliance, but returns the original string. Such as:

Copy Code code as follows:

' Ab CD '. Title ()--> ' AB CD '//The first letter of each word in the string
' China ab 123CD '. Title ()--> ' China ab 123Cd '///Even if the first character is not an alphabetic character, it can be converted
' China 123 '. Title ()--> ' China 123 '

Str.center (Width[,fillchar]):

Returns a new string with a width of the original string that is greater than Len (str) or returns the original string, which is populated with Fillchar at the beginning and end of the string, and the default is a space.
Note: When the width is even, the Fillchar will be evenly populated to the beginning and end of the original string, and the Fillchar precedence fills the front when it is odd. Such as:

Copy Code code as follows:

' ABCD '. Center (3)--> ' ABCD '
' ABCD '. Center (8)--> ' ABCD '
' ABCD '. Center (8, *)--> ' **abcd** '
' ABCD '. Center (7, *)--> ' **abcd* '

Str.ljust (width[, Fillchar]):

Returns a string with a width of length, left-aligned, Fillchar to the right, and a space by default. Width is greater than Len (str), otherwise the original string is returned. Such as:

Copy Code code as follows:

' ABCD '. Ljust (a)--> ' ABCD '

Str.rjust (width[, Fillchar]):

Similar to Str.ljust (), but it returns a right-aligned string, with the leftmost padding Fillchar.

Str.lstrip ([chars]):

Returns a new string that removes the leading character, the chars parameter is a string that contains all the character sets that will be removed. The default is a space.
Note: On the Lstrip function (including the Rstrip and strip), there are many articles on the Internet, but they are not clear. What it actually means is that, starting at the far left of the original string, matches all the characters contained in the chars until the first chars character is encountered, all the characters in the original string are removed.

Copy Code code as follows:

' www.example.com '. Lstrip (' Cmowz. ') -->example.com

Starting at the leftmost end of the string until the chars character E is encountered, a total of 3 W characters and one. Character is encountered, and the e-match ends.
Copy Code code as follows:

' Xyxxyy Testyx yx yyx '. Lstrip (' xy ')--> ' Testyx yx '

Matches from the leftmost end of the string until it encounters a chars character T, which matches three x three Y, and a space, and encounters a T-match ending.

Str.rstrip ([chars]):
Instead of Str.lstrip (), match from the far right.

Copy Code code as follows:

' Xyxxyy Testyx yx yyx '. Rstrip (' xy ')--> ' XYXXYY test '

Str.strip ([chars]):
Start a match from both ends of the string.

Copy Code code as follows:

' Xyxxyy Testyx yx yyx '. Strip (' XY ')-->test

Str.expandtabs ([tabsize]):
Replaces all tabs in a string with 0 or more spaces, and each tab is replaced by a number of spaces, which are determined by the position of the tab in the string and the tabsize. TABSIZE Specifies the number of spaces to be replaced by each tab, with a default of 8. Such as:

Copy Code code as follows:

' \t\t this\tis test. ' Expandtabs (8)--> ' This is test. '

In the example above, the first two \ t, each replaced with 8 spaces, and the third T appears to be replaced by 4. In fact, because tab stops are calculated from the beginning of each line, the tab stop for the Third tab is the 24th position from the beginning of the start, just before the I of IS, not the 8th position from the beginning of this. This is called a joint decision.

Str.zfill (width):
Returns a numeric string with a length of width, with the leftmost padding of 0. If width is less than equal to the length of the original string, the original string is returned. It is mainly used in the format of numeric string. Such as:

Copy Code code as follows:

' abc '. Zfill (5)--> ' 00abc '//generally do not do this format, it doesn't make sense
' 123 '. Zfill (5)--> ' 00123 '

Find & Replace class methods:

Str.count (sub[, start[, end]]):

Counts the number of substrings in a substring of a character. Start and end specify the range of statistics, and the default full string range is unspecified. Such as:

Copy Code code as follows:

' Abcdabac '. Count (' ab ')-->2
' Abcdabac '. Count (' AB ', 2,)-->1

Str.find (sub[, start[, end]]):
Finds the first position that a substring appears in a string, and start and end specify a lookup range. No return-1 was found.

Copy Code code as follows:

' 0123234 '. Find ('-->2 ')
' 0123234 '. Find (' 1 ")-->2

Note: 1, find looks for a substring in the full string in the first position, matching to the end of the string to find, regardless of whether there is a matching string behind.
2. Find finds that the substring is the first position in the full string, not the first in the specified slice.
3, if only to determine whether the substring in a string, with the in-judge can be, do not need to find.

Str.rfind (sub[, start[, end]]):
As with the Find method, returns the index position of the specified substring, except that rfind from the far right of the string and returns-1 if it is not found. Note: Find from the far right, but the index position is calculated from the far left of the original string. Such as:

Copy Code code as follows:

' Abcdeef '. Find (' E ')-->4//lookup from leftmost, ending with E from a to first d, returning index value 4
' Abcdeef '. RFind (' e ')-->5//start looking from the far right, ending with E from a to the first F, returning index value 5

Str.format (*args, **kwargs):
There is not only plain text in the string that invokes the Fortmat method, but also substitution fields that are included with the {} qualifier. A replacement field can be either a numeric index of positional parameters or a key value for a dictionary or property. In the string returned by the method, all substitution fields are replaced by the values of the corresponding parameters. Such as:

Copy Code code as follows:

' User ID: {0} '. Format (' root ')-->user Id:root
' User ID: {uid} ' last login: {last_login} '. Format (UID = ' root ', Last_login = ' 5 Mar ')-->user Id:root last Lo Gin:5 Mar 2012

Str.index (sub[, start[, end]]):

Similar to Str.find (), but if no substring is found, return raised valueerror.

Str.rindex (sub[, start[, end]]):

Similar to Str.rfind (), but if not found, return raises valueerror.

Str.replace (old, new[, Count]):
Returns a new string in which the old in the original string is replaced with the new,country specify the number of substitutions. Such as:

Copy Code code as follows:

' AAABBBCCC '. Replace (' A ', ' D ')-->DDDBBBCCC
' AAABBBCCC '. Replace (' A ', ' D ', 2)-->DDABBBCCC

Static Str.maketrans (x[, [Y, z]]):
I don't quite understand this method, especially if it has a static modifier.
In general, its purpose is to return a conversion table for use by the Str.translate () method, and two methods are used in conjunction.
Such as:

Copy Code code as follows:

Table = Str.maketrans (' cs ', ' KZ ')
"Please don ' t knock in my door!".   Translate (table)--> "Pleaze don t Knokk at my door!" The ' C ' was replaced with K, and ' s ' was replaced with Z. The description parameter can contain more than one character, but the first parameter contains the number of characters and the second parameter must have equal number of characters.

Table = Str.maketrans (' cs ', ' kz ', ' O ')
"Please don ' t knock in my door!".   Translate (table)--> "Pleaze dn ' t knkk at my dr!" If there are three parameters, the third argument means that the corresponding characters in the original string are deleted.

Str.translate (map):

In conjunction with the Str.maketrans () function, replace the corresponding character.


Split & Combine class methods:

Str.partition (Sep):

This method is used to split the string and returns a tuple containing three elements. If Sep is not found in the original string, the three elements of the tuple are: the original string, the empty string, the empty string; otherwise, the first Sep character encountered in the original string is split, and the three elements of the tuple are: the string before the Sep, the Sep character, and the string after the Sep;

Copy Code code as follows:

' Abcdee '. partition (' F ')--> (' Abcdee ', ', ', ')
' Abcdee '. partition (' E ')--> (' ABCD ', ' e ', ' e ')

Str.rpartition (Sep):

In contrast to Str.partition (), the split is started from the far right of the original string, but also returns a tuple that contains three elements: the string before the penultimate Sep, the Sep character, and the string after the Sep.
Notice the "string before the countdown to Sep," which is the previous string, starting at the far left of the original string, not the rightmost. Such as:

Copy Code code as follows:

The ' Abcdee '. Rpartition (' e ')--> (' ABCDE ', ' e ', ')//split three elements are: the elements before the last E, E itself, the elements after e, and the spaces
' Abcdee '. Rpartition (' F ')--> (', ', ' Abcdee ')//split three elements are: space, space, original string

Str.split ([sep[, Maxsplit]]):

Returns a list of Sep-delimited, maxsplit specifies the number of splits (therefore, the number of elements in the list is Maxsplit + 1). Sep defaults to spaces, and maxsplit does not limit the number of splits by default.
Note: 1 The spaces on both sides of STR are discarded if SEP is not specified or if Sep is None ('), and if Sep is specified (whether or not it can be found in the original string), the spaces on both sides of the Str are preserved
2 if Sep is not found in the original string, a list containing only one element is returned, and the element is the original string.
Such as:

Copy Code code as follows:

' Abcbdbee '. Split ()--> [' Abcbdbee ']///unspecified Sep, returns a list containing only one element, discarding the spaces on both sides of the Str
' Abcbdbee '. Split (' F ')--> [' Abcbdbee ']///Specify F as Sep (although no f is found), returns a list containing only one element, preserving the spaces at both ends
' Abcbdbee '. Split (' B ')--> [' A ', ' C ', ' d ', ' ee ']///Specify B as Sep, no limit on the number of splits, and the spaces on both sides of STR are retained
' Abcbdbee '. Split (' B ', 2)--> [' A ', ' C ', ' Dbee ']//= split two times with B as separator

Note: A bit like str.partition (), but str.partition () returns a tuple, and the delimiter Sep is an element in the tuple; Str.split (0 Returns a list, the separator Sep is not in the list

Str.rsplit ([sep[, Maxsplit]]):

Similar to Str.split (), except that it splits from the far right. You will see the effect only if you specify Maxsplit. Such as:

Copy Code code as follows:

' Abcbdbee '. Rsplit (' B ')--> [' A ', ' C ', ' d ', ' ee ']//Do not specify Maxsplit, return the same result as Str.split ()
' Abcbdbee '. Rsplit (' B ', 2)--> [' abc ', ' d ', ' ee ']//can see the difference with str.split (' B ', 2)

Str.join (iterable):

Uses the connector str to connect elements in the Iterable object, and returns a string of elements of the Iterable object that is connected by Str. If you pass in a Iterable object, such as an integer, a Boolean value, and so on, the type Error is returned. Such as:

Copy Code code as follows:

' A B '. Join ([' 1 ', ' 2 ', ' China '])-->1a B2A b China
' A B '. Join (' 12 China ')-->1a B2A b China
' A B '. Join (123)-->type Error

Note: The main feature of Iterable object or iterator type is support for two functions: __iter__ () and __next__ (), although not very accurate, it is easy to assume that data types that support the use of a for statement-by-value are iterator objects.
Sequence type (six: Strings, byte objects, byte arrays, lists, tuples, Range objects), and dictionary all belong to the Iterable object.

Str.splitlines ([keepends]):

Splits a string that contains multiple lines, returning a list with one element per act. If the string is not multiline, the original string is returned. Keepends is a true character or a non-0 integer that preserves the End-of-line flag. This method is used for processing files. Such as:

Copy Code code as follows:

line = ' AB
Cd
EF ""
Line.splitlines ()-->[' AB ', ' CD ', ' EF '

line = ' Ab\ncd\nef '
Line.splitlines ()-->[' AB ', ' CD ', ' EF '

line = ' Ab\ncd\nef '
Line.splitlines (True)--> [' ab\n ', ' cd\n ', ' EF ']

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.