The method of string, notes and Example 2.

ljust (width) Returns a string left-aligned and padded with spaces to the width of the length of the new string. / Dʒʌst / adj fair and reasonable; the upright, and justice; right; fair; just adv deserve only; just now, just; precisely, exactly; it; just to

>>> str1 = 'python'
>>> str1.ljust(20,'0')
'python00000000000000'

lstrip () remove all spaces string left. 

>>> str1 = '     Python'
>>> str1.lstrip()
'Python'

 Partition (sub) find the sub-string sub, the string into a 3-tuple (pre_sub, sub, fol_sub), if the string is not included in the sub returns ( 'original string', '', '') / pɑr 'tɪʃən / n. division, separate

>>> str1 = 'python'
>>> str1.partition('z')
('python', '', '')
>>> str1.partition('t')
('py', 't', 'hon')

Replace (old, new [, count]) replace the old sub-string to string new substring, if the count is specified, alternatively no more than count times. . / Rɪ'ples / vt substituted in place; Alternatively, replacement; restitution, reimbursement; ... put back in place

>>> str1 = 'ababababab'
>>> str1.replace('a','s',4)
'sbsbsbsbab'

rfind (sub [, start [, end]]) similar to the find () method, but is beginning to find the right.

rindex (sub [, start [, end]]) similar to the index () method, but starting from the right.

the rjust (width) Returns a string right justified and padded with spaces to the width of the length of the new string. And ljust () is similar.

rpartition (sub) similar to the partition () method, but is beginning to find the right.

rstrip () to delete the spaces at the end of the string.

split (sep = None, maxsplit = -1) with no parameters is a list of default spliced ​​substring after the separator to space string sections, if maxsplit parameters set, only the partition maxsplit substrings, returns slice.

/ splɪt /  . n-cleavage; adj fracture split vt separation;.. the separation; cleavage; away; vi exploded away;. cleaved; disown

>>> str1 = 'Change the world by program.'
>>> str1.split()
['Change', 'the', 'world', 'by', 'program.']
>>> str1.split('e')
['Chang', ' th', ' world by program.']

splitlines (([keepends])) in the output if there newline removed, the default is False, it does not contain a newline; If True, then the reserved line breaks. . / Splɪt / / lainz / n lines; lines; route (a plurality of line)

. V lined; Videos in line (in the form of a single line of three)

>>> str1 = 'Change \nthe \nworld \nby \nprogram.'
>>> str1
'Change \nthe \nworld \nby \nprogram.'
>>> print(str1)
Change 
the 
world 
by 
program.
>>> str1.splitlines()
['Change ', 'the ', 'world ', 'by ', 'program.']
>>> str1 = 'Change the world by program.'
>>> str1.splitlines()
['Change the world by program.']

startswith (prefix [, start [, end]]) checks whether the string starts with prefix, it returns True, otherwise False. parameters may specify start and end range check, optional. . / stɑrt / vt start; start.

/ Swɪθ / ADV. Immediately, quickly

>>> str1 = 'Change the world by program.'
>>> str1.startswith('C')
True
>>> str1.startswith('C',10,15)
False

strip ([chars]) to delete all the strings front and back spaces, chars parameter can be customized character deleted, optional. / Strɪp /

  

>>> str1 = '    abcdefgabc     '
>>> str1.strip()
'abcdefgabc'
>>> str1.strip('a')
'    abcdefgabc     '
>>> str2 = 'aaabbbcccaaa'
>>> str2.strip('a')
'bbbccc'
>>> str3 = 'aaabbbccc'
>>> str3.strip('a')
'bbbccc'

swapcase () in the case of reversing the string. . / Swɑp / v exchange, exchange; transactions; in exchange for ......;. Instead / kes / n where; examples; Box

>>> str1 = 'PYthon'
>>> str1.swapcase()
'pyTHON'

 

upper () converts the string in all lowercase characters to uppercase.

title () returns the title (all the words are in uppercase start, the rest of the letters are lowercase) string.

Translate (table) in accordance with rule table (it may be made str.maketrans ( 'a', customized 'b')) of the character string conversion. . / Træns'let / vt translation; conversion; interpretation; into; to mobilize

>>> str3 = 'aaabbbccc'
>>> str3.translate(str.maketrans('a', 's') )
'sssbbbccc'

zfill (width) Returns a string of length to width, the original string is right-justified with zero-padded front.

Reference: https: //fishc.com.cn/thread-38992-1-1.html

 

 

Guess you like

Origin www.cnblogs.com/ztmboke/p/11240385.html