day2 string method summary

字符串 str() 包含的方法:
'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper','zfill'

Case conversion
capitalize()&title() casefold()&lower()&upper()
s = 'ab cD EF'
print(s.capitalize()) #The first letter is capitalized
print(s.title()) #The first letter Uppercase
casefold()& lower() #The difference between lowercase characters
is: the lower() method is only valid for ASCII encoding, that is, 'A-Z', and converts uppercase to lowercase in other languages ​​(non-Chinese or English) In cases where only the casefold() method can be used.
upper() #Character case
swapcase() #Reverse the case of the string

Align fill
center() ljust() rjust() zfill()
#S.center(width[, fillchar]) -> str
res = s.center(20,'#') #Center alignment fill.
print(res)
ljust( ) #Left
justify fill
s = 'a'
print(s.ljust(6,'#'))
rjust() #Right
justify fill
print(s.rjust(6,'#'))
zfill () #The method returns a string of the specified length, the original string is right-aligned, and the front is filled with 0.
Remove spaces and specified characters
strip() lstrip() rstrip()
Remove spaces at the left and right ends of a string, or specify a string
s = '@@a@@'
print(s.lstrip('@'))
print(s.rstrip ('@'))
print(s.strip('@'))

split string
split() rsplit() splitlines()
s = 'a@@a@@a@@c\ndsadsa@@w'
print( s.split('@@',1)) #Separate according to the given string, return a list, you can specify the number of times. Split from left to right.
print(s.
print(s.splitlines()) The
partition()
method is used to split the string according to the specified separator. Divided into three parts, the left of the separator, the separator, and the right of the separator.
s ='dsafsasadsavdab'
print(s.partition('a')) #from left to right
print(s.rpartition('a'))#from right to left

Check the
isalnum() method to detect if the string consists of letters and numbers.
The isalpha() method detects if a string consists of only letters.
The isdigit() method detects if a string consists of digits only.
The islower() method detects whether the string consists of lowercase letters.
The isnumeric() method checks if a string consists of numbers only. This method is only for unicode objects.
The isspace() method detects if a string consists of spaces only.
The istitle() method checks whether all words in the string are spelled with the first letter in uppercase and all other letters in lowercase.
The isupper() method checks if all letters in a string are uppercase.
The isidentifier() method detects if a string is a python reserved word.
isprintable() checks if a string is a printable character. (In ASCII, No. 0 to 32 and No. 127 are control characters; No. 33 to 126 are printable characters)
endswith & startswith Check what characters start and end with, and return bool

Find substring position
find() index() rfind() rindex()
s = '@@a@@a@@'
print(s.find('a',2,5)) #You can also set the range. Find from the left, minimize the search.
print(s.index('a'))#Find the index of the string, if not, report an error
print(s.rfind('a')) #Find the specified string from the right. Maximize search.
print(s.rindex('a'))

Count
count()
#S.count(sub[, start[, end]]) -> int
s = 'aaabbcccccddd'
res = s.count('cc',0,2) #return the number of strings in the original string Number

character encoding
encode() decode() #String
encoding, transit encoding unicode. It needs to be decoded to unicode before transcoding.
Python strings use unicode encoding by default.
s = 'character encoding'
s1 = s.encode('GBK')
print(s1)
s2 = s1.decode('GBK')
print(s2)
s3 = s1.decode('utf-8')
print(s3)

replace

The replace()
method replaces the old (old string) in the string with new (new string). If the third parameter max is specified, the replacement will not exceed max times.
str.replace(old, new[, max])
s1='aaaaa' print(s1.replace('a','b',2))


The join()
method is used to connect the elements in the sequence with the specified characters to generate a new string .
s = '#'
s1 = ('a','b','c')
print(s.join(s1))

Modify the length of the tab key in the string
expandtabs
s='name\temal\tpassword\nzhangsan\[email protected]\t123\nlisi\[email protected]\t123\nzhangsan\[email protected]\t123\n'
v = s.expandtabs(20)#How many characters to expand the tab, count together with the characters before \t. Can be tabulated.
print(v)

conversion
translate() &maketrans()
translate() method converts the characters of the string according to the table given by the parameter table (contains 256 characters), and puts the characters to be filtered out into the deletechars parameter.
str.translate(table)
bytes.translate(table[, delete])
bytearray.translate(table[, delete])
s = 'abcs'
s1='1234'
str = 'aasdvvvccc' #
tran = str.maketrans(s, s1) # Make a mapping table
print(str.translate(tran))#Conversion

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325257523&siteId=291194637