String function learning Summary In Python3.2, python3.2 string

Source: Internet
Author: User
Tags printable characters string methods

String function learning Summary In Python3.2, python3.2 string

Sequence Types

There are six sequence types: strings, byte sequences (bytes objects), byte arrays (bytearray objects), list, tuple, range objects.

Common operations supported by sequence:
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

Class method, usually returns a Boolean value:

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

Returns True or False if the string ends with a specified suffix. Start and end specify the start range. The default value is the full string. For example:
Copy codeThe Code is as follows:
'Abcde'. endswith ('de') --> True
'Abcde'. endswith ('de', 0, 3) --> Flase


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

Opposite to str. endwith (), judge whether the string starts with a specified prefix.

Str. islower ():

This method is used to determine whether all the letter characters in a string are in lowercase. Only the letter characters in the string are determined, and other characters are ignored. The string must contain at least one letter; otherwise, False is returned. For example:
Copy codeThe Code is as follows:
'China'. islower () --> False
'AB China'. islower () --> True

Str. isupper ():

Unlike the st. islower () method, this method determines whether all letter characters are capitalized.

Str. istitle ():

Determines whether the first letter of each word in a string is capitalized. The string must contain at least one letter; otherwise, False is returned. Even if the first letter contains non-letter characters, such as Chinese characters, numbers, and underscores, it does not affect the determination of the first letter.
Copy codeThe Code is as follows:
'China'. istitle () --> False // The string does not contain letters, and False is returned.
'Abc of China '. istitle () --> True // although the first letter A contains non-letter characters, True is returned.
'-Abc xyz'. istitle () --> False // The first letter of the last word is not capital, and False is returned.

Str. isalnum ():

Determines whether a string contains only numeric characters, and whether the string contains only Chinese characters. If the string contains spaces, underscores, and ,~ Returns False if it is a non-literal numeric character. For example:
Copy codeThe Code is as follows:
'3'. isalnum () --> True
'China'. isalnum () --> True
'-'. Isalnum () --> False

Note: alphanumberic is a special word. It indicates that the character string consists of numbers or characters. For example, '3' contains a number character, 'A' contains a text character, and '3a 'also contains both a number character and a letter character.

Str. isalpha ():
Determines whether a string contains only Chinese characters. For example:
Copy codeThe Code is as follows:
'China'. isalpha () --> True
'3'. isalpha () --> False

Str. isidentifier ():

Determines whether a string is a valid identifier. A string only contains Chinese characters. In fact, it determines whether the variable name is valid. For example:
Copy codeThe Code is as follows:
'_ A'. isidentifier () --> True
'3a '. isidentifier () --> False
'China'. isidentifier () --> True

Str. isprintable ():

Determines whether all characters in a string are printable. A string contains non-printable characters. If it is an escape character, False is returned.

Str. isspace ():

Determines whether a string contains only spaces or tabs. Note: The space character is different from the space character, for example:
Copy codeThe Code is as follows:
''. Isspace () --> False
''. Isspace () --> True

Str. isdecimal ():

Determines whether a string contains only decimal numeric characters, including decimal numeric characters in multiple languages. For example:
Copy codeThe Code is as follows: '3'. isdecimal () --> True
'\ U0660'. isdeciaml () --> True

Other language decimal digit form reference: http://www.fileformat.info/info/unicode/category/Nd/list.htm

Str. isdigit ():

Determines whether a string contains only numbers. The numbers here include decimal numbers and other special numbers (such as Upper-mark numbers ). Generally, a number is a character with 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. Generally, Numeric characters have the following attribute values: Numeric_Type = Digit, Numeric_Type = Decimal or Numeric_Type = Numeric.
Compared with isdecimal (), isdigit (), and isnumeric (), the detection range of several methods is expanded in sequence.


The formatting method returns a formatted New String:

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

Encode the string in UTF-8 format.

Str. lower ():

Converts all letter characters to lowercase letters without any other non-letter characters. It is also legal that all strings are non-letter characters, but the original string is returned. For example:
Copy codeThe Code is as follows:
'China 123abc'. lower () --> 'China 123abc'
'China'. lower () --> 'China' 123 '// No error is returned, the original string is returned

Str. upper ():

Contrary to str. lower (), convert all letter characters into uppercase letters. For example:
Copy codeThe Code is as follows:
'China 123abc'. upper () --> 'China 123abc'
'China 100'. upper () --> 'China 100'

Str. swapcase ():

Replace uppercase and lowercase letters in a string, convert them to lowercase letters, and convert them to uppercase letters. Do not care about non-letter characters. For example:
Copy codeThe Code is as follows:
'China 123ab'. swapcase () --> 'China 123ab'
'China' 123 '. swapcase () --> 'China' 123' // No error is returned, the original string is returned

Str. capitalize ():

The first letter of the string is uppercase, And the other lowercase letters are used. If the string starts with a non-letter character, the original string is returned. A string contains only non-letter characters, but returns the original string. For example:
Copy codeThe Code is as follows:
'AB Cd'. capitalize () -->' AB Cd' // only converts the first letter of the string.
'China AB 123cd'. capitalize () --> 'China AB 123cd' // the first character is a non-letter character and the original string is returned.
'China'. capitalize () --> 'China' 123 '// No error is returned, the original string is returned

Str. title ():

The first letter of each word in the string is uppercase, And the other lowercase letters are used. Conversion is not affected if the word starts with a non-letter character. A string contains only non-letter characters, but returns the original string. For example:
Copy codeThe Code is as follows:
'AB Cd'. title () -->' AB Cd' // uppercase of each word in the string
'China AB 123cd'. title () --> 'China AB 123cd' // can be converted even if the first character is a non-letter character.
'China 123 '. title () --> 'China 123'

Str. center (width [, fillchar]):

Returns a new string of the original string centered with the length of width. The width must be greater than len (str). Otherwise, the original string is returned. The original string starts and ends with fillchar, the default value is space.
Note: If the width is an even number, fillchar will evenly fill the beginning and end of the original string. If the width is an odd number, fillchar will first fill the front. For example:
Copy codeThe Code is as follows:
'Abc'. center (3) --> 'abc'
'Abc'. center (8) --> 'abc'
'Abcd'. center (8, *) --> '** abcd **'
'Abcd'. center (7, *) --> '** abcd *'

Str. ljust (width [, fillchar]):

Returns a string with the length of width and left alignment. It is filled with fillchar on the rightmost side. The default value is space. Width must be greater than len (str); otherwise, the original string is returned. For example:
Copy codeThe Code is as follows:
'Abc'. ljust (10) --> 'abc'

Str. Fill ust (width [, fillchar]):

Similar to str. ljust (), but it returns a right-aligned string with fillchar on the left.

Str. lstrip ([chars]):

Returns a new string that removes the leading characters. The chars parameter is a string that contains all the character sets to be removed. The default value is space.
Note: There are many articles on lstrip functions (including rstrip and strip), but they are not clear. It actually means that, starting from the leftmost of the original string, it matches all characters contained in chars until the first non-chars character is met, all matching characters in the original string are removed.
Copy codeThe Code is as follows:
'Www .example.com '. lstrip ('cmowz.') --> example.com

Matching starts from the leftmost of the string until a non-chars character e is encountered. A total of three w characters and A. character are matched, and the matching ends when e is encountered.
Copy codeThe Code is as follows:
'Xyxxyy testyx yyx'. lstrip ('xy') --> 'testyx yyx'

Matching starts from the leftmost of the string until it encounters a non-chars character t. It matches three x three y characters and a space. t matching ends.

Str. rstrip ([chars]):
Opposite to str. lstrip (), match from the rightmost.
Copy codeThe Code is as follows:
'Xyxxyy testyx yyx'. rstrip ('xy') --> 'xyxxyy Test'

Str. strip ([chars]):
Starts from the two ends of the string.
Copy codeThe Code is as follows:
'Xyxxyy testyx yyx'. strip ('xy') --> test

Str. expandtabs ([tabsize]):
Replace all the tabs in the string with zero or multiple spaces. The number of spaces for each tab is determined by the position of the tab in the string and the tabsize. Tabsize specifies the number of spaces to replace each tab. The default value is 8. For example:
Copy codeThe Code is as follows:
'\ T this \ tis test.'. expandtabs (8) --> 'this is test .'

In the above example, each of the first two \ t is replaced with eight spaces, and the third \ t seems to be replaced with only four. In fact, it is not because the tabs start from the beginning of each line. Therefore, the third tab is located at the first position of the row, just before the is I, instead of the first location after this. This is the so-called joint decision.

Str. zfill (width ):
Returns a string of numbers with a length of width. The leftmost value is 0. If the width is less than or equal to the length of the original string, the original string is returned. It is mainly used for formatting numeric strings. For example:
Copy codeThe Code is as follows:
'Abc'. zfill (5) --> '00abc' // This format is generally not used and does not make any sense.
'123'. zfill (5) --> '123'

Search and replace methods:

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

Counts the number of sub characters in a character string. Start and end specify the statistical range. If not specified, statistics are collected within the full string range by default. For example:
Copy codeThe Code is as follows:
'Abcac'. count ('AB') --> 2
'Abcac'. count ('AB', 2,) --> 1

Str. find (sub [, start [, end]):
The first position where the substring appears in the string. start and end specify a search range. -1 is not found.
Copy codeThe Code is as follows:
'123'. find ('23') --> 2
'123'. find ('23', 1) --> 2

Note: 1. find finds the first position of the substring in the full string. If it matches the string, it ends the search, regardless of whether there is any matched string.
2. find finds the first position of the substring in the whole string, rather than the first position in the slice.
3. If you only want to determine whether the Sub-string is in a certain string, use the in operator, and do not need to find.

Str. rfind (sub [, start [, end]):
Like the find method, the index position of the specified substring is returned, except that rfind starts searching from the rightmost side of the string and-1 is returned when it is not found. Note: The search starts from the rightmost, but the index position starts from the leftmost of the original string. For example:
Copy codeThe Code is as follows:
'Abcdeef '. find ('E') --> 4 // start searching from the leftmost, end E after A to the first D, and return the index value 4
'Abcdeef '. rfind ('E') --> 5 // search from the rightmost, from A to E before the first F, return the index value 5

Str. format (* args, ** kwargs ):
The string that calls the fortmat method contains both plain text and replacement fields included with the {} delimiter. The replacement field can be either a numerical index of the location parameter or a dictionary or a Key value of the attribute. All the replacement fields in the string returned by this method are replaced by the values of corresponding parameters. For example:
Copy codeThe 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 2012 ') --> User ID: root Last login: 5 Mar 2012

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

Similar to str. find (), but if no substring is found, raised ValueError is returned.

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

Similar to str. rfind (), but if not found, raises ValueError is returned.

Str. replace (old, new [, count]):
Returns a new string. The old value in the original string is replaced with new, and the country value specifies the number of replicas. For example:
Copy codeThe Code is 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 it has a static modifier.
Generally, it is used to return a conversion table for the str. translate () method. The two methods are often used together.
For example:
Copy codeThe 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' is replaced with k, and 's' is replaced with z. The parameter can contain multiple characters, but the number of characters contained in the first parameter must be equal to the number of characters contained in the second parameter.

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 to delete the corresponding characters in the original string.

Str. translate (map ):

Used with the str. maketrans () function to replace the corresponding characters.


Splitting and combination methods:

Str. partition (sep ):

This method is used to split a string and return a triple containing three elements. If Sep cannot be found in the original string, the three elements of the tuples are: original string, empty string, and empty string. Otherwise, the values are split from the first Sep character in the original string, the three elements of the tuples are: the string before Sep, the Sep character, and the string after Sep; for example:
Copy codeThe Code is as follows:
'Abcdee'. partition ('F') --> ('abcdee ','','')
'Abcdee'. partition ('E') --> ('abcd', 'E', 'E ')

Str. rpartition (sep ):

And str. partition (), on the contrary, is split from the rightmost of the original string, but returns the tuples containing three elements: the string before the last Sep, the Sep character, and the string after the Sep.
Note the "string before the last Sep". The previous string starts from the leftmost of the original string, not the rightmost. For example:
Copy codeThe Code is as follows:
'Abcdee '. rpartition ('E') --> ('abcde', 'E', '') // The three elements to be split are the elements before the last e, e itself, the elements after e, and spaces
'Abcdee'. rpartition ('F') --> ('','', 'abcdee') // The three elements to be split are space, space, and original string.

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

Returns a list separated by Sep. maxsplit specifies the number of splits (therefore, the number of elements in the list is maxsplit + 1 ). The default value of Sep is space. maxsplit does not limit the number of splits by default.
Note: 1) If no Sep is specified or the specified Sep is None (''), spaces at both ends of str are discarded. If you specify Sep (whether or not Sep can be found in the original string ), spaces at both ends of str will be retained
2) If Sep cannot be found in the original string, a list containing only one element is returned. This element is the original string.
For example:
Copy codeThe Code is as follows:
'Abcbdbee '. split () --> ['abcbdbee'] // if no Sep is specified, a list containing only one element is returned, discarding spaces at both ends of str
'Abcbdbe '. split ('F') --> ['abcbdbee '] // specify f as Sep (although f cannot be found), return a list containing only one element, and retain spaces at both ends
'Abcbdbe '. split ('B') --> ['A', 'C', 'D', 'ee'] // specify B as Sep, which does not limit the number of splits, spaces at both ends of str are retained
'Abcbdbe'. split ('B', 2) --> ['A', 'C', 'dbe'] // use the separator 'B' for splitting twice

Note: It is a bit like str. partition (), but str. partition () returns a tuples, And the separator Sep is an element in the tuples; and str. split (0 returns a list. The separator Sep is not in the list.

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

Similar to str. split (), but it is split from the rightmost. The results are only displayed when maxsplit is specified. For example:
Copy codeThe Code is as follows:
'Abcbdbe '. rsplit ('B') --> ['A', 'C', 'D', 'ee '] // If maxsplit is not specified, the returned result is. same as split ()
'Abcbdbe '. rsplit ('B', 2) --> ['abc', 'D', 'ee '] // you can see it with str. split ('B', 2)

Str. join (iterable ):

Use the connector str to connect the elements in the iterable object and return a string consisting of elements of the iterable object connected by str. If a non-iterable object is input, such as an integer or Boolean value, Type Error is returned. For example:
Copy codeThe 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: iterable object or iterator type supports two major features: __iter _ () and _ next _ (). Although they are not accurate, however, we can simply think that the data types that support the for statement values one by one are all 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 rows and returns a list of each element. If the string is not multiline, the original string is returned. Keepends is a True character or a non-zero integer, indicating that the end flag of the row is retained. This method is mostly used to process files. For example:
Copy codeThe 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']

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.