Summary of Python built-in string methods, python strings
String processing is a very common skill, but there are too many built-in string methods in Python that are often forgotten. For quick reference, I wrote examples for each built-in method based on Python 3.5.1 and classified them, easy for indexing.
PS: You can click the green title in the overview to enter the corresponding category or use the quick indexing method in the text directory on the right bar.
Case-sensitive Conversion
Str. capitalize ()
Convert the first letter to uppercase. Note that if the first letter is not in uppercase, the original string is returned.
'Adi dog'. capitalize ()
# 'Adi dog'
'Abcd Xu '. capitalize ()
# 'CD Xu'
'Xu abcd'. capitalize ()
# 'Xu abcd'
'Authorization'. capitalize ()
# 'Ss'
Str. lower ()
Converts a string to lowercase letters, which are only valid for ASCII characters.
'Dobi'. lower ()
# 'Dobi'
'Lower '. lower () # 'lower' is a lower-case German letter with another lower-case 'ss'. The lower method cannot be converted.
# 'Signature'
'Xu abcd'. lower ()
# 'Xu abcd'
Str. casefold ()
Converts a string to lowercase letters. Unicode encoding converts a string to lowercase letters.
'Dobi'. casefold ()
# 'Dobi'
'Authorization'. casefold () # lowercase English letters (ss) are equivalent to lowercase letters (SS ).
# 'Ss'
Str. swapcase ()
Returns the uppercase and lowercase letters of a string.
'Xu Dobi a123 done'. swapcase ()
#: 'Xu dOBI A123 ss' the token here is converted to SS in uppercase.
Note that s. swapcase (). swapcase () = s is not necessarily true:
U' \ xb5'
# 'Micro'
U' \ xb5 '. swapcase ()
# 'Signature'
U' \ xb5 '. swapcase (). swapcase ()
# 'μ'
Hex (ord (u '\ xb5'. swapcase (). swapcase ()))
Out [154]: '0x3bc'
Here, the lowercase letters of 'signature' (mu is not M) are exactly the same as those of 'μ.
Str. title ()
Capital the first letter of each "word" in the string. Its judgment of "word" is based on space and punctuation. Therefore, an error may occur when all spaces or abbreviations in English are written.
'Hello world'. title ()
# 'Hello world'
'Chinese abc def 12gh'. title ()
# 'Chinese Abc Def 12gh'
# But this method is not perfect:
"They're bill's friends from the UK". title ()
# "They're Bill's Friends From The Uk"
Str. upper ()
If all letters in a string are converted to uppercase letters, the system automatically ignores the characters that cannot be converted to uppercase letters.
'Chinese abc def 12gh'. upper ()
# 'Chinese abc def 12gh'
Note that s. upper (). isupper () is not necessarily set to True.
String format output
Str. center (width [, fillchar])
The string is centered according to the given width. A specific character can be filled with extra length. If the specified length is smaller than the string length, the original string is returned.
'123'. center (10 ,'*')
# '** 12345 ***'
'123'. center (10)
#'123'
Str. ljust (width [, fillchar]); str. Fill ust (width [, fillchar])
Returns a string of the specified length. If the string content is left (right) and the length is smaller than the string length, the original string is returned. The default value is ASCII space. You can specify the string to be filled.
'Dobi'. ljust (10)
# 'Dobi'
'Dobi'. ljust (10 ,'~ ')
# 'Dobi ~~~~~~ '
'Dobi'. ljust (3 ,'~ ')
# 'Dobi'
'Dobi'. ljust (3)
# 'Dobi'
Str. zfill (width)
Fill the string with '0' and return the string of the specified width.
"42". zfill (5)
#'123'
"-42". zfill (5)
# '-000000'
'Dd'. zfill (5)
# '000dd'
'--'. Zfill (5)
#'-000 -'
''. Zfill (5)
#'123'
''. Zfill (5)
#'123'
'Ddddddddd'. zfill (5)
# 'Ddddddddddd'
Str. expandtabs (tabsize = 8)
Use the specified space to replace the horizontal tab to keep the spacing between adjacent strings within the specified space.
Tab = '1 \ t23 \ t456 \ t7890 \ t1112131415 \ t161718192021'
Tab. expandtabs ()
#'1 23 456 7890 1112131415 161718192021'
# '123' pay attention to the relationship between the space count and the output position above
Tab. expandtabs (4)
#'1 23 456 7890 1112131415 161718192021'
#'123'
Str. format (^ args, ^ kwargs)
The syntax for formatting strings is quite wide. The official documentation already provides more detailed examples. Here we will not write an example. To learn more, you can directly stamp Format examples here.
Str. format_map (mapping)
Similar to str. format (* args, ** kwargs), mapping is a dictionary object.
People = {'name': 'john', 'age': 56}
'My name is {name}, I am {age} old '. format_map (People)
# 'My name is john, I am 56 old'
Search, locate, and replace strings
Str. count (sub [, start [, end])
Text = 'outer protective covering'
Text. count ('E ')
#4
Text. count ('E', 5, 11)
#1
Text. count ('E', 5, 10)
#0
Str. find (sub [, start [, end]); str. rfind (sub [, start [, end])
Text = 'outer protective covering'
Text. find ('er ')
#3
Text. find ('to ')
#-1
Text. find ('er', 3)
Out [121]: 3
Text. find ('er', 4)
Out [122]: 20
Text. find ('er', 4, 21)
Out [123]:-1
Text. find ('er', 4, 22)
Out [124]: 20
Text. rfind ('er ')
Out [125]: 20
Text. rfind ('er', 20)
Out [126]: 20
Text. rfind ('er', 20, 21)
Out [129]:-1
Str. index (sub [, start [, end]); str. rindex (sub [, start [, end])
Similar to find () rfind (), the difference is that if it cannot be found, ValueError is triggered.
Str. replace (old, new [, count])
'Dog wow jiao '. replace ('wow', 'wang ')
# 'Dog wang jiao'
'Dog wow jiao '. replace ('wow', 'wang', 1)
# 'Dog wang wow jiao'
'Dog wow jiao '. replace ('wow', 'wang', 0)
# 'Dog wow jiao'
'Dog wow jiao '. replace ('wow', 'wang', 2)
# 'Dog wang jiao'
'Dog wow jiao '. replace ('wow', 'wang', 3)
# 'Dog wang jiao'
Str. lstrip ([chars]); str. rstrip ([chars]); str. strip ([chars])
'Dobi'. lstrip ()
# 'Dobi'
'Db .kun.ac.cn '. lstrip ('dbk ')
# '.Kun.ac.cn'
'Dobi'. rstrip ()
# 'Dobi'
'Db .kun.ac.cn '. rstrip ('acn ')
# 'Db. kun. ac .'
'Dobi'. strip ()
# 'Dobi'
'Db .kun.ac.cn '. strip ('db. C ')
# 'Kun .ac.cn'
'Db .kun.ac.cn '. strip ('cbd. un ')
# 'Un.'
Static str. maketrans (x [, y [, z]); str. translate (table)
Maktrans is a static method used to generate a comparison table for translate.
If there is only one maktrans parameter, this parameter must be a dictionary. The dictionary key is either a Unicode encoded (an integer) or a string with a length of 1, the dictionary value can be any string, None, or Unicode encoding.
A = 'dobi'
Ord ('O ')
#111
Ord ('A ')
#97
Hex (ord ('Dog '))
#'0x72d7'
B = {'D': 'dobi', 111: 'Is ',' B ': 97,' I ':' \ u72d7 \ u72d7 '}
Table = str. maketrans (B)
A. translate (table)
# 'Dobi is a dog'
If maktrans has two parameters, the two parameters form a ing and the two strings must be of the same length. If there is a third parameter, the third parameter must also be a string, the string will be automatically mapped to None:
A = 'dobi is a dog'
Table = str. maketrans ('dobi', 'alph ')
A. translate (table)
# 'Alph hs a alg'
Table = str. maketrans ('dobi', 'alph', 'O ')
A. translate (table)
# 'Aph hs a AG'
String union and Segmentation
Str. join (iterable)
Concatenates an element with a specified string as a string's iteratable object.
'-'. Join (['123', '3'])
#'2017-3-12'
'-'. Join ([2012, 3, 12])
# TypeError: sequence item 0: expected str instance, int found
'-'. Join (['123', '3', B '12']) # bytes is a non-string.
# TypeError: sequence item 2: expected str instance, bytes found
'-'. Join (['123'])
#'123'
'-'. Join ([])
#''
'-'. Join ([None])
# TypeError: sequence item 0: expected str instance, NoneType found
'-'. Join ([''])
#''
','. Join ({'dobi': 'Dog', 'polil': 'bird '})
# 'Dobi, polil'
','. Join ({'dobi': 'Dog', 'polil': 'bird'}. values ())
# 'Dog, bird'
Str. partition (sep); str. rpartition (sep)
'Dog wow jiao'. partition ('wow ')
# ('Dog ', 'wow', 'Wow jiao ')
'Dog wow jiao'. partition ('Dog ')
# ('', 'Dog', 'Wow wow jiao ')
'Dog wow jiao'. partition ('jiao ')
# ('Dog wow', 'jiao ','')
'Dog wow jiao'. partition ('ww ')
# ('Dog wow jiao ','','')
'Dog wow jiao'. rpartition ('wow ')
Out [131]: ('dog wow', 'wow', 'jiao ')
'Dog wow jiao'. rpartition ('Dog ')
Out [132]: ('', 'dog ', 'Wow wow jiao ')
'Dog wow jiao'. rpartition ('jiao ')
Out [133]: ('dog woww', 'jiao ','')
'Dog wow jiao'. rpartition ('ww ')
Out [135]: ('','', 'dog wow jiao ')
Str. split (sep = None, maxsplit =-1); str. rsplit (sep = None, maxsplit =-1)
'1, 2,3 '. split (', '), '1, 2, 3'. rsplit ()
# (['1', '2', '3'], ['1, ', '2,', '3'])
'1, 2,3 '. split (', ', maxsplit = 1), '1, 2,3'. rsplit (',', maxsplit = 1)
# (['1', '2, 3'], ['1, 2', '3'])
'1 2 3'. split (), '1 2 3'. rsplit ()
# (['1', '2', '3'], ['1', '2', '3'])
'1 2 3'. split (maxsplit = 1), '1 2 3'. rsplit (maxsplit = 1)
# (['1', '2 3'], ['1 2', '3'])
'1 2 3'. split ()
# ['1', '2', '3']
'1, 2, 3, '. split (', '), '1, 2, 3,'. rsplit (',')
# (['1', '2', '', '3',''], ['1', '2', '', '3 ', ''])
''. Split ()
# []
''. Split ('A ')
# ['']
'Bcd'. split ('A ')
# ['Bcd']
'Bcd'. split (None)
# ['Bcd']
Str. splitlines ([keepends])
The string is split into a list using the line delimiter. When keepends is set to True, the line Delimiter is retained after the string is split. For the line delimiters that can be recognized, see the official documentation.
'AB c \ n \ nde fg \ rkl \ r \ n'. splitlines ()
# ['AB C', '', 'de fg', 'kl']
'AB c \ n \ nde fg \ rkl \ r \ n'. splitlines (keepends = True)
# ['AB c \ n',' \ n', 'de fg \ R', 'kl \ r \ n']
"". Splitlines (), ''. split ('\ n') # note the differences between the two.
# ([], [''])
"One line \ n". splitlines ()
# (['One Line'], ['two lines', ''])
String condition judgment
Str. endswith (suffix [, start [, end]); str. startswith (prefix [, start [, end])
Text = 'outer protective covering'
Text. endswith ('in ')
# True
Text. endswith ('gin ', 'in '))
# True
Text. endswith ('ter ', 2, 5)
# True
Text. endswith ('ter ', 2, 4)
# False
Str. isalnum ()
Any combination of string and number is true, in short:
If any one of c. isalpha (), c. isdecimal (), c. isdigit (), c. isnumeric () is true, c. isalnum () is true.
'Dobi'. isalnum ()
# True
'Bi123'. isalnum ()
# True
'200'. isalnum ()
# True
'Xu '. isalnum ()
# True
'Dobi _ 100'. isalnum ()
# False
'Dobi 123 '. isalnum ()
# False
'%'. Isalnum ()
# False
Str. isalpha ()
The Unicode Character database is used as a "Letter" (these characters are generally identified by "Lm", "Lt", "Lu", "Ll", or "Lo", different from Alphabetic) is true.
'Dobi'. isalpha ()
# True
'Do bi'. isalpha ()
# False
'Bi123'. isalpha ()
# False
'Xu '. isalpha ()
# True
Str. isdecimal (); str. isdigit (); str. isnumeric ()
The difference between the three methods is that the true value of the Unicode universal logo has different ranges:
Isdecimal: Nd,
Isdigit: No, Nd,
Isnumeric: No, Nd, Nl
The difference between digit and decimal is that some numeric strings are digit but not decimal.
Num = '\ u2155'
Print (num)
# Renewal
Num. isdecimal (), num. isdigit (), num. isnumeric ()
# (False, False, True)
Num = '\ u00B2'
Print (num)
#2
Num. isdecimal (), num. isdigit (), num. isnumeric ()
# (False, True, True)
Num = "1" # unicode
Num. isdecimal (), num. isdigit (), num. isnumeric ()
# (True, True, True)
Num = "'7 '"
Num. isdecimal (), num. isdigit (), num. isnumeric ()
# (False, False, True)
Num = "10"
Num. isdecimal (), num. isdigit (), num. isnumeric ()
# (False, False, True)
Num = B "1" # byte
Num. isdigit () # True
Num. isdecimal () # AttributeError 'bytes 'object has no attribute 'isdecimal'
Num. isnumeric () # AttributeError 'bytes 'object has no attribute 'isnumeric'
Str. isidentifier ()
Determines whether a string can be a valid identifier.
'Def '. isidentifier ()
# True
'With'. isidentifier ()
# True
'False'. isidentifier ()
# True
'Dobi _ 000000'. isidentifier ()
# True
'Dobi 123 '. isidentifier ()
# False
'200'. isidentifier ()
# False
Str. islower ()
'Xu '. islower ()
# False
'Duration'. islower () # German capital letters
# False
'A Xu '. islower ()
# True
'Ss'. islower ()
# True
'23'. islower ()
# False
'AB'. islower ()
# False
Str. isprintable ()
Determines whether all characters in a string are printable or the string is null. The characters in the "Other" and "Separator" categories in the Unicode Character Set are non-printable characters (but do not include ASCII spaces (0x20 )).
'Bi123'. isprintable ()
# True
'Dobi123 \ n'. isprintable ()
Out [24]: False
'Dobi 123 '. isprintable ()
# True
'Dobi. 100'. isprintable ()
# True
''. Isprintable ()
# True
Str. isspace ()
Determines whether the string contains at least one character, and all characters are blank characters.
In [29]: '\ r \ n \ t'. isspace ()
Out [29]: True
In [30]: ''. isspace ()
Out [30]: False
In [31]: ''. isspace ()
Out [31]: True
Str. istitle ()
To determine whether a character in a string is capitalized, non-letter characters are ignored.
'How Python works'. istitle ()
# True
'How Python works'. istitle ()
# False
'How python works'. istitle ()
# False
'How Python works'. istitle ()
# True
''. Istitle ()
# False
''. Istitle ()
# False
'A'. istitle ()
# True
'A'. istitle ()
# False
'Shake Abc Def 123 '. istitle ()
# True
Str. isupper ()
'Xu '. isupper ()
# False
'Dobi'. isupper ()
Out [41]: True
'Dobi'. isupper ()
# False
'Bi123'. isupper ()
# True
'Dobi 123 '. isupper ()
# True
'Dobi \ t 100'. isupper ()
# True
'Dobi _ 000000'. isupper ()
# True
'_ 100'. isupper ()
# False
String Encoding
Str. encode (encoding = "UTF-8", errors = "strict ")
Fname = 'xu'
Fname. encode ('ascii ')
# UnicodeEncodeError: 'ascii 'codec can't encode character '\ u5f90 '...
Fname. encode ('ascii ', 'replace ')
# B '? '
Fname. encode ('ascii ', 'ignore ')
# B''
Fname. encode ('ascii ', 'xmlcharrefresh ')
# B 'Xu'
Fname. encode ('ascii ', 'backslashreplace ')
# B '\ u5f90'
Articles you may be interested in:
- Python character string encode and decode research experience garbled Problem Solution
- How to merge connection strings in python list
- How to remove spaces from strings in Python
- Python string operations
- Python's method of determining whether a string is a pure number
- Python method for determining whether a string contains a substring
- How to convert strings and dates in python
- Use of the replace () method for string operations in Python
- How to remove spaces at both ends of a string using Python
- How to calculate the frequency of occurrence of words in a text string in python
- Python implements the method of searching and outputting the same characters in two strings
- How to filter single quotes in mysql using python
- Conversion of Python strings, tuples, lists, and dictionaries