Python string manipulation methods in detail

Source: Internet
Author: User

A string sequence is used to represent and store text, and a string in Python is an immutable object. A string is a collection of ordered characters that is used to store and represent basic textual information, a pair of single, double, or triple quotation marks that are contained in the middle of what is called a string. Where three quotation marks can be composed of multiple lines, writing the shortcut syntax of multiple lines of text, common document string, at a specific location of the file, as a comment. Convenient multi-line comments.

Python actually has three types of strings:

1. Usual meaning string (str)

2. Original string, starting with uppercase R or lowercase R, R ', not escaping special characters

3.Unicode string, u ' basestring sub-class

Strings in Python support indexing, slicing operations.

According to the python3.x string source file visible, a total of 44 methods, find listed as follows:

Method Description

String.capitalize ()

Capitalize the first character of a string

String.center (width)

Returns the center of the original string and fills the new string with a space of length width

String.count (str, beg=0, End=len (String))

Returns the number of occurrences of STR in a string, if beg or end specifies that the number of STR occurrences in the specified range is returned

String.decode (encoding= ' UTF-8 ', errors= ' strict ')

Decodes a string in the encoded format specified by encoding, if an error defaults to a ValueError exception unless errors specifies ' ignore ' or ' replace '

String.encode (encoding= ' UTF-8 ', errors= ' strict ')

Encodes a string in the encoding format specified by encoding, if an error defaults to a ValueError exception unless errors specifies ' ignore ' or ' replace '

String.endswith (obj, beg=0, End=len (String))

Checks whether the string ends with obj, or returns False if beg or end specifies whether to end with obj in the specified range, or True if it is.

String.expandtabs (tabsize=8)

Turns the tab symbol in string strings to a space, and the default number of spaces for the tab symbol is 8.

String.find (str, beg=0, End=len (String))

Detects if STR is contained in a string, and if beg and end specify a range, the check is contained within the specified range, and returns 1 if the index value is returned.

String.index (str, beg=0, End=len (String))

Just like the Find () method, only if STR does not report an exception in string.

String.isalnum ()

If the string has at least one character and all characters are letters or numbers, return

Return True, otherwise False

String.isalpha ()

Returns True if the string has at least one character and all characters are letters.

otherwise returns False

String.isdecimal ()

Returns True if the string contains only a decimal number, otherwise False.

String.isdigit ()

Returns True if the string contains only a number, otherwise False.

String.islower ()

Returns True if the string contains at least one case-sensitive character, and all of these (case-sensitive) characters are lowercase, otherwise False

String.isnumeric ()

Returns True if the string contains only numeric characters, otherwise False

String.isspace ()

Returns True if the string contains only spaces, otherwise False is returned.

String.istitle ()

Returns True if string is heading (see Title ()), otherwise False

String.isupper ()

Returns True if the string contains at least one case-sensitive character, and all of these (case-sensitive) characters are uppercase, otherwise False

String.Join (seq)

Merges all the elements in the SEQ (the string representation) into a new string, using string as a delimiter

String.ljust (width)

Returns the left alignment of an original string and fills the new string with the width of length with a space

String.Lower ()

Converts all uppercase characters in a string to lowercase.

String.lstrip ()

Truncate the left space of a string

String.maketrans (Intab, Outtab])

The Maketrans () method is used to create a conversion table of character mappings, for the simplest invocation of two parameters, the first argument is a string that represents the character that needs to be converted, and the second argument is the target of the string representation of the transformation.

Max (str)

Returns the largest letter in the string str .

Min (str)

Returns the smallest letter in the string str .

String.partition (str)

A bit like the combination of find () and split (), from the first position where Str appears, divide the string into a 3-element tuple (STRING_PRE_STR,STR,STRING_POST_STR), if string STR is not included in the STRING_PRE_STR = = string.

String.Replace (str1, str2, Num=string.count (STR1))

Replace the str1 in string with the str2, if NUM is specified, the replacement is no more than num times.

String.rfind (str, Beg=0,end=len (String))

Similar to the Find () function, it is just looking from the right.

String.rindex (str, Beg=0,end=len (String))

Similar to index (), but starting from the right.

String.rjust (width)

Returns the right alignment of the original string and fills the new string with the width of the length with a space

String.rpartition (str)

Similar to the partition () function, it is just looking from the right.

String.rstrip ()

Removes a space at the end of a string string.

String.Split (str= "", Num=string.count (str))

Slice string with a delimiter of STR, if NUM has a specified value, only the NUM substring is delimited

String.splitlines (Num=string.count (' \ n '))

Returns a list that contains rows as elements, separated by rows, and if num specifies that only num rows are sliced.

String.startswith (obj, Beg=0,end=len (string))

Checks whether the string starts with obj, returns True, or False. If beg and end specify a value, the check is within the specified range.

String.strip ([obj])

Execute Lstrip () and Rstrip () on string

String.swapcase ()

Flip the case in a string

String.title ()

A string that returns "header", meaning that all words start with uppercase and the remaining letters are lowercase (see istitle ())

String.translate (str, del= "")

Converts a string of characters according to the table given by STR, which contains 256 characters,

The characters to filter out are placed in the Del parameter

String.upper ()

Convert lowercase letters in string to uppercase

String.zfill (width)

Returns a string of length width, the original string is right-aligned, and the front padding is 0

String.isdecimal ()

The Isdecimal () method checks whether a string contains only decimal characters. This method exists only in Unicode objects.

This table refers to the https://www.cnblogs.com/A-FM/p/5691468.html, mainly because of its method points in, there is a detailed explanation, reference to come, mainly for the convenience of everyone.

Summing up the above usage, we can divide the string operations into string substitution, deletion, interception, copying, connection, comparison, lookup, segmentation, etc.

However, the first thing to judge is whether the object is a string, as follows:

How to tell if an object is not a string

There are double strings in Python, one is str and one is Unicode. So how do you tell if an object is a string? You should use Isinstance (s,basestring) instead of Isinstance (S,STR). Look at the following example

>>>a = ' Hi ' >>>isinstance (a,str) true>>>b = U ' Hi ' >>>isinstance (b,str) False> >>isinstance (b,basestring) true>>>isinstance (B,unicode) true>>>isinstance (A,unicode) False

To correctly interpret whether an object is a string, basestring, because Basestring is the base class for STR and Unicode, and contains ordinary strings and Unicode types.

The basic usage of the Str object is as follows

The basic usage of the string can be divided into the following five categories, that is, nature judgment, search and replace, cut and connect, deformation, fill in the blanks and limitations.

(a) Nature determination

There are several methods to determine the nature.

    Isalnum (): Whether all letters and numbers, and at least one character    Isalpha (): Is all letters, and at least one character    isdigit (): Is all numbers, and has at least one character    Islower () : Whether the letters in the string are all lowercase    isupper (): Whether the letters in the string are all lowercase    isspace (): are all whitespace characters and have at least one character    Istitle () : Determines whether a string has every word and only the first letter is uppercase    StartsWith (Prefix[,start[,end]): Used to check whether the string starts with the specified substring, or True if it is, otherwise False. If the parameter start and end specify a value, the check is within the specified range.    EndsWith (Suffix[,start[,end]): Used to determine whether the string ends with the specified suffix, or false if the end of the specified suffix returns true. The optional parameter "start" and "end" are the starting and ending positions of the retrieved string.    is* () These are relatively simple, from the letter can be literally understood that the *with () function can accept the start and end parameters, if the good use can optimize performance. Also, from the Python version 2.5, the first parameter of the *with () function family can accept a tuple type argument, which returns True when an element in the argument matches

(b) Find and replace

    Count (Sub[,start[,end]]): Counts the number of occurrences of a character sub in a string. The optional parameters are the start and end positions of the string search. This value is useful when calling the Replace method.    find (Sub[,start[,end]): Detects whether a substring sub is contained in the string, if the start (start) and end (end) ranges are specified, the check is included in the specified range, and returns 1 if the containing substring returns the starting index value.    index (Sub[,start[,end]): Just like the Find () method, except if the sub does not throw a ValueError exception in the string.    RFind (Sub[,start[,end]): Similar to the Find () function, but looks from the right.    Rindex (Sub[,start[,end]): Similar to index (), but starts from the right. Replace    (Old,new[,count]): Used to replace some substrings of a string, replacing old with new. If the Count argument is specified, the maximum number of count times is replaced, and if not specified, all are replaced

The first five methods can accept the start and end parameters, and good use can optimize performance. The index family and the find family methods are not recommended for finding substrings in a string, and in and not are recommended for use in operations

(c) Slitting and linking

    Partition (SEP): Used to split the string according to the specified delimiter, if the string contains the specified delimiter, returns a tuple of $3, the first is the 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 Sep does not appear in the string, the return value is (Sep, ","). The partition () method was added in version 2.5.    rpartition (Sep): Similar to the partition () function, but looks from the right.    Splitness ([keepends]): Separated by rows (' \ r ', ' \ r \ n ', \ n '), returns a list containing the rows as elements, if the argument        keepends is False, does not contain a newline character, and if true, the newline character is preserved.    split (Sep[,maxsplit]): Slices a string by specifying a delimiter and, if the parameter maxsplit has a specified value, separates only Maxsplit substrings, returning the segmented list of strings.    Rsplit (Sep[,maxsplit]): Same as split (), but starting from the right.

  

Look at the following example

str1 = ' ab c\n\nde fg\rkl\r\n ' Print str1.splitlines (); str2 = ' ab c\n\nde fg\rkl\r\n ' Print str2.splitlines (True)

The output is as follows:

[' Ab C ', ', ' de fg ', ' KL '] [' Ab c\n ', ' \ n ', ' de fg\r ', ' kl\r\n ']

The split () function has a small trap, such as a different return value for the string S,s.split () and S.split (""). Look at the code below

s = "Hello World" #前面有两个空格print s.split () print S.split (")

  

The output is as follows

[' Hello ', ' world '] [', ', ', ' Hello ', ' world ']

What is this for? The reason for this is that split () employs two different algorithms when the SEP parameter is ignored or when the Sep parameter is none and the string value is given to Sep explicitly. For the former, split () removes the whitespace characters from both ends, and then takes the whitespace string of any length as the delimiting character (that is, the continuous whitespace string is treated as a single white space character), and for the latter, it is considered that there is an empty character between two consecutive whitespace characters, so the return value is different for the empty string.

>>> ". Split () []>>>". Split (") ["]

(d) Deformation

    Lower (): Converts all uppercase characters in a string to lowercase.    Upper (): Converts lowercase letters in a string to uppercase.    capitalize (): capitalizes the first letter of a string, and other letters to lowercase. For 8-bit byte encoding needs to be based on the local environment.    swapcase (): Used to convert the uppercase and lowercase letters of a string, upper case to uppercase, and lowercase.    title (): Returns the "heading" string, meaning that all words start with uppercase and the remaining letters are lowercase.

These are uppercase and lowercase switches, title () does not remove whitespace from both ends of the string and does not replace contiguous whitespace with a space, and if so, you can use the Capwords (s) function of the string module to remove both whitespace characters. and can replace the continuous whitespace with a space character. Look at the following example:

#coding =utf-8import strings = "Hello World" Print s.title () print string.capwords (s)

  

The output results are as follows:

Hello Worldhello World

(e) Limitation and filling

    Strip ([chars]): Used to remove the character specified by the tail of the string (the default is a space), if more than one will be deleted.    Ltrip ([chars]): Used to truncate the left space of a string or specify a character.    Rtrip ([chars]): Used to truncate the space to the right of a string or specify a character.    Center (Width[,fillchar]): Returns the center of the original string and fills the new string with the width of length with Fillchar. The default padding character is    a space ljust (Width[,fillchar]): Returns an original string left aligned and uses Fillchar to fill a new string of the specified length by default to a space. Returns the original string if the specified length is less than the length of the original string.    Rjust (Width[,fillchar]): Returns an original string right-aligned and uses Fillchar to fill a new string of length width. Returns the original string if the specified length is less than the length of the string.    Zfill (width): Returns a string of the specified length, the original string right-aligned, preceded by 0    expandtabs ([tabsize]): the tab symbol (' \ t ') in the string is converted to the appropriate number of spaces, by default, to 8.

(f) String slicing

str = ' 0123456789′print Str[0:3] #截取第一位到第三位的字符print str[:] #截取字符串的全部字符print str[6:] #截取第七个字符到结尾print str[:-3] # Intercept from the beginning to the third character before print str[2] #截取第三个字符print str[-1] #截取倒数第一个字符print str[::-1] #创造一个与原字符串顺序相反的字符串print str[-3:-1] # Intercept the character before the last third and last digit print str[-3:] #截取倒数第三位到结尾print str[:-5:-3] #逆序截取, intercept between the fifth and last digits of the penultimate number

  

Python string manipulation methods in detail

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.