A summary of the learning of string functions in Python3.2

Source: Internet
Author: User
Tags alphabetic character iterable string methods
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 the sequence type:
Member check: in, not in
Connection: +
Copy: *
Subscript value: S[i]
Slice: s[i:j]
Length check: Len (s)
Minimum value: Min (s)
Maximum value: Max (s)
Index value: S.index (i)
String statistics: S.count (i)

String Methods

To determine a class method, you typically return a Boolean value:

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

Determines whether the string ends with the specified suffix and returns True or false. Start and end specify the starting range of the judgment, the default full string. Such as:

The code is 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 the alphabetic characters in a string are all lowercase, and the method only determines the alphabetic characters in the string, ignoring other characters. The string must contain at least one alphabetic character, otherwise it will return false. Such as:

The code is as follows:


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

Str.isupper ():

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

Str.istitle ():

Determines whether the first letter of each word in the string is capitalized. The string must contain at least one alphabetic character, otherwise it will return false. Even if the first letter character is preceded by non-alphabetic characters, such as Chinese, numerals, underscores, etc., it does not affect the judgment of the first letter character.

The code is as follows:


' China '. Istitle ()-->false//string contains no letters and returns FALSE
' China ABC '. Istitle ()-->true//Although the first letter character A is preceded by a non-alphabetic character, it still returns True
'-abc xyz '. Istitle ()-->false//After the first letter of a word is not uppercase, returns False

Str.isalnum ():

Determines whether a string contains only literal numeric characters, and the string contains only the Chinese text in accordance with the method. Returns False if the string contains non-literal numeric characters, such as spaces, underscores, ~, and so on. Such as:

The code is 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, and ' 3a ' includes both numeric and alphabetic characters.

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

The code is as follows:


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


str.isidentifier ():

Determines whether a string is a valid identifier, and the string contains only Chinese text conformance, which in fact determines whether the variable name is legal. Such as:

The code is as follows:


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


Str.isprintable ():

Determines whether the characters contained in the string are all printable. The string contains a non-printable character, such as an escape character, that returns false.

Str.isspace ():

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

The code is as follows:


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

Str.isdecimal ():

Determines whether a string contains only decimal numeric characters, including the decimal numeric character representation of multiple languages. Such as:

The code is 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 numbers. In general, a number is a character that has the following attribute values: Numeric_type=digit or Numeric_type=decimal.

Str.isnumeric ():

Determines whether a string contains only numeric characters. The numeric character range is large, in general, numeric characters are characters that have the following attribute values: Numeric_type=digit, Numeric_type=decimal, or numeric_type=numeric.
Compared with Isdecimal (), IsDigit (), IsNumeric (), the range of detection of several methods is expanded in turn.


Format the class method, returning a new formatted string:

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

Encodes the string in utf-8 format.

Str.lower ():

Converts all alphabetic characters to lowercase, without any other non-alphabetic characters. Strings are all non-alphabetic characters and are valid, but return the original string. Such as:

The code is as follows:


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

Str.upper ():

In contrast to Str.lower (), converts all alphabetic characters to uppercase. Such as:

The code is as follows:


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

Str.swapcase ():

Swaps the uppercase and lowercase letters in the string, capitalization into lower case, lowercase to uppercase. Do not take care of non-alphabetic characters. Such as:

The code is as follows:


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


str.capitalize ():

The first letter of the string is capitalized and the remaining lowercase. If the first character of the string is a non-alphabetic character, the original string is returned. The string contains only non-alphabetic word compliance, but returns the original string. Such as:

The code is as follows:


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

Str.title ():

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

The code is as follows:


' Ab CD '. Title ()--' AB CD '//capitalize 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 centered in the original string with width greater than Len (str), otherwise returning the original string, populated with Fillchar at the beginning and end of the original string, and the default is a space.
Note: When width is even, Fillchar will be evenly filled to the beginning and end of the original string, and for odd numbers, the Fillchar first fills the front. Such as:

The code is as follows:


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

Str.ljust (width[, Fillchar]):

Returns a width, left-aligned string, the rightmost padding Fillchar, and the default is a space. Width is greater than Len (str), otherwise the original string is returned. Such as:

The code is as follows:


' ABCD '. Ljust (Ten)---' 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 before the chars parameter is a string that contains all the character sets that will be removed. The default is a space.
Note: About Lstrip functions (including Rstrip and strip), there are many articles on the Internet, but they are not clear. It actually means, starting at the far left of the original string, matching all the characters contained in the chars until the first non-chars character is encountered, all the characters in the original string are removed.

The code is as follows:


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


Matches from the leftmost side of the string until the non-chars character e is encountered, matching 3 W characters and one. character, and encountering the end of the E match.

The code is as follows:


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


Match from the far left of the string until the non-chars character T is encountered, altogether matching three x three Y, and a space, and encountering the T match end.

Str.rstrip ([chars]):
In contrast to Str.lstrip (), matches are started from the far right.

The code is as follows:


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

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

The code is as follows:


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

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

The code is as follows:


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


In the above example, the first two \ T, each replaced by 8 spaces, and the third \ T appears to have replaced only 4. In fact, because tab stops are counted from the beginning of each line, the tab stop of the Third tab is the 24th position starting at the beginning of the row, just before the is I, not the 8th position from the beginning of this. This is the so-called common decision.

Str.zfill (width):
Returns a numeric string of length width, with the leftmost padding 0. If width is less than or equal to the original string length, the original string is returned. Used primarily for the formatting of numeric class strings. Such as:

The code is as follows:


' abc '. Zfill (5)--' 00abc '//generally not do this format, it makes no sense
' 123 '. Zfill (5)--' 00123 '

Find & Replace class methods:

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

Counts the number of sub-strings in a character. Start and end specify the statistic range, which is not specified by default full string in-range statistics. Such as:

The code is as follows:


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

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

The code is as follows:


' 0123234 '. Find ('-->2 ')
' 0123234 '. Find (' All ', 1)-->2


Note: 1. Find finds a substring in the first position that appears in the full string, matching the string to the end of the lookup, regardless of whether or not there is a matching string behind it.
2. Find finds a substring in the first position of the full string, rather than the first position in the specified slice.
3. If you want to determine whether a substring is in a string, use the in-judging character, and do not need find.

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

The code is as follows:


' Abcdeef '. Find (' E ')-->4//From the leftmost start, from a to the end of E after the first D, returns the index value 4
' Abcdeef '. RFind (' E ')-->5//from the far right to find, from a to the end of the first F in front of E, return the index value 5

Str.format (*args, **kwargs):
There is not only plain text in the string that calls the Fortmat method, but also substitution fields that are included with the {} delimiter. A replacement field can be either a numeric index of a positional parameter 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:

The code is 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 Login: 5 Mar 2012

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

Similar to Str.find (), but returns raised ValueError if no substring is found.

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

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

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

The code is as follows:


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

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

The code is as follows:


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

Table = Str.maketrans (' cs ', ' kz ', ' O ')
"Please don ' t knock at my door!". Translate (table)--"Pleaze DN ' t knkk at my dr!"//If there are three parameters, the third parameter means deleting the corresponding character in the original string.

Str.translate (map):

Use with the Str.maketrans () function to replace the corresponding character.


Split & Combine class methods:

Str.partition (Sep):

This method is used to split a string, returning a tuple that contains three elements. If Sep is not found in the original string, the three elements of the tuple are: original string, empty string, empty string; otherwise, the first Sep character encountered in the original string begins splitting, and the three elements of the tuple are: a string before Sep, a Sep character, a string after sep, such as:

The code is as follows:


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

Str.rpartition (Sep):

In contrast to Str.partition (), splitting begins at the far right of the original string, but also returns a tuple containing three elements: the string before the first Sep, the Sep character, and the string after Sep.
Note that the "string before the end of Sep", the previous string, is calculated from the leftmost end of the original string, not the rightmost one. Such as:

The code is as follows:


' Abcdee '. Rpartition (' e ')--(' ABCDE ', ' e ', ')//split three elements, respectively: the element before the first E-Countdown, E itself, the element after E, and also the space
' Abcdee '. Rpartition (' F ')--(', ', ' Abcdee ')//split three elements: space, space, original string

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

Returns a list that is separated by Sep, maxsplit Specifies the number of splits (therefore, the number of elements in the list is Maxsplit + 1). Sep defaults to a space, and maxsplit does not limit the number of splits by default.
Note: 1) If Sep is not specified or Sep is None (' '), the spaces at both ends of STR are discarded, and if Sep is specified (regardless of whether Sep is found in the original string), the spaces at both ends of STR are preserved
2) If Sep is not found in the original string, a list containing only one element is returned, and this element is the original string.
Such as:

The code is as follows:


' Abcbdbee '. Split ()--[' Abcbdbee ']//unspecified Sep, returns a list containing only one element, discarding the spaces at both ends of STR
' Abcbdbee '. Split (' F ')--[' Abcbdbee ']//Specifies that f is 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, do not limit the number of splits, and the spaces at both ends of STR are retained
' Abcbdbee '. Split (' B ', 2)--[' A ', ' C ', ' Dbee ')//with B as delimiter, split two times

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 delimiter Sep is not in the list

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

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

The code is 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 ']//You can see the difference with str.split (' B ', 2)

Str.join (iterable):

Use the connector str to concatenate the elements in the Iterable object and return a string of elements of the Iterable object that are concatenated by Str. If you pass in a non-iterable object, such as an Integer, Boolean, and so on, the type Error is returned. Such as:

The code is 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 the Iterable object or iterator type is that it supports two functions: __iter__ () and __next__ (), although not very accurate, but it is simple to assume that data types that support the use of a for-statement-by-one value are iterator objects.
Sequence type (six types: Strings, byte objects, byte arrays, lists, tuples, Range objects) and dictionary belong to iterable objects.

Str.splitlines ([keepends]):

Splits a string that contains multiple lines, returning a list for each element of the behavior. If the string is not multi-line, the original string is returned. Keepends is a true character or a non-0 integer that represents a reserved end-of-line flag. This method is used for processing files. Such as:

The code is 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 ']

  • 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.