—————————str(string) ————————
capitalize
test = 'asdasdwqd'
v = test.capitalize() #capitalize the first letter
print(v)
Result: Asdasdwqd
casefold / lower / upper / swapcase / title
test = 'ASDDWASD WasdafqwASD' v = test.casefold() #All letters are turned into lowercase, casefold, use casefold for Unicode print(v) v1 = test.lower() #All letters are changed to lowercase, lower() is only valid for ASCII or 'AZ' print(v1) v2 = test.upper() #all letters are changed to lowercase print(v2) v3 = test.swapcase() #change all uppercase to lowercase, lowercase to uppercase print (v3) v4 = test.title() #capitalize the first letter print (v4) result: asddwasd wasdafqwasd asddwasd wasdafqwasd ASDDWASD WASDAFQWASD asddwasd wASDAFQWasd Asddwasd Wasdafqwasd
center / bright / right
test = 'wASD' v = test.center(20,'*') #To print 20 characters is not enough, use * to complete from both sides to the center print(v) test = "qiqiqiq" v = test.ljust(20,"*") #Total printing 20 characters is not enough to complete with * sign from the right print(v) v2 = test.rjust(20,"*")#Total printing 20 characters is not enough to complete with * sign from the left print(v2) result: ********wASD******** qiqiqiq ************* ************* qiqiqiq
count
test = 'wsdadwdadasdasdasdasdASD' v = test.count('a',3,10) #Count the number of occurrences of a character in the string. Optional arguments are the start and end positions in the string search print(v) Result: 3
endswith / startswith
test = "alex" v = test.endswith('ex') # end with what print(v) v = test.startswith('ex') # what to start with print(v) result: True False
expandtabs
test = "ale\tx\nale\tx\nale\tx\n" v = test.expandtabs(20)#If there is \t in the string, then \t will be converted to 20 spaces when printing print(v) result: ale x ale x ale x
find / rfind
test = "my name is " v = test.find('m') # Starting from subscript 0, find the first occurrence of the substring in the string, return the result: 0 print(v) v = test.find('m',1) # starting from index 1, find the first occurrence of the substring in the string: return result 5 print(v) v = test.find('z') # return -1 if not found print(v) v1 = test.rfind('m') #Search from the right, return result: 5 print(v1) v1 = test.rfind('m',5) #Start searching from the fifth subscript on the right, and return the result: 5 print(v1) result: 0 5 -1 5 5
format / format_map
test = "my name is {name} , is ago {year}" print(test) v = test.format(name='qitian',year=23)#Format, replace the placeholder in a string with the specified value print(v) test = 'i am {0}, age {1}' print(test) v= test.format('qitian',19)#Format, replace the placeholder in a string with the specified value print(v) test = "my name is {name} , is ago {year}" print(test) v = test.format_map( {'name': 'qitian', 'year': '23'} ) # format, passed in value print(v) result: my name is {name} , is ago {year} my name is qitian , is ago 23 i am {0}, age {1} i am qitian, age 19 my name is {name} , is ago {year} my name is qitian , is ago 23
index / rindex
test = 'asdasdasdasdwq' v = test.index('d',1,4) #If the search subscript does not exist, it cannot be found, and an error is reported. You can define the start position and end position to search for the subtitle d from Table 1 below, and the end position is shown in the following table 4 print(v) v = test.rindex('d',1,6) #returns the last position of the substring d in the string test print(v) result: 2 5
join / strip / rstrip / lstrip
test = 'aaaasdasdasdasdwqaaa' print(test) v = list(test) #convert to list print(v) v2 = "".join(test) #Convert to string print(type(v2)) #View the type print(v2) v3 = v2.strip("a") #Remove two paragraphs of characters a The default is a space print (v3) v4 = v2.rstrip("a") #Remove the right side print (v4) v5 = v2.lstrip("a") #Remove the left print(v5) result: aaaasdasdasdasdwqaaa ['a', 'a', 'a', 'a', 's', 'd', 'a', 's', 'd', 'a', 's', 'd', 'a', 's', 'd', 'w', 'q', 'a', 'a', 'a'] <class 'str'> aaaasdasdasdasdwqaaa sdasdasdasdwq aaaasdasdasdasdwq sdasdasdasdwqaaa
maketrans / translate
v = str.maketrans("abcdefj","1234567") #Generate a conversion table, one-to-one correspondence. test = "bceja" print(test.translate(v)) #Use the created mapping table, test to convert the string result: 23571
partition / rpartition
test = "1a2a3a4a5a6a7a8a9a" v = test.partition("6a") # Specified separator. 6a print(v) #Return a 3-tuple, 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. print(v[1]) v1 = test.rpartition("8a") print(v1) print(v1[1]) result: ('1a2a3a4a5a', '6a', '7a8a9a') 6a ('1a2a3a4a5a6a7a', '8a', '9a') 8a
replace
test = "abcdefgabcdefg" v = test.replace('abcd','1234',2) #Replace, replace abcd with 12234, parameter 2, is the amount of replacement print(v) result: 1234efg1234efg
split / rsplit
test = "asdawdqdasdqwxsxqwd" v = test.split("d") print(v) test = "asdawdqdasdqwxsxqwd" v = test.rsplit("w",1) print(v) The # method splits the string by specifying the delimiter and returns a list. The default delimiter is all empty characters, including spaces, newlines (\n), tabs (\t), etc. Similar to the split() method, but splits from the end of the string. sep -- optional parameter, the specified separator, the default is all empty characters, including spaces, newlines (\n), tabs (\t), etc. count -- optional parameter, the number of splits, the default is the total number of times the separator appears in the string. result: ['as', 'aw', 'q', 'as', 'qwxsxqw', ''] ['asdawdqdasdqwxsxq', 'd']
splitlines
test = 'ab c\n\nde fg\rkl\r\n' print(test.splitlines()) test1 = 'ab c \ n \ nde fg \ rkl \ r \ n' print(test1.splitlines(True)) #Whether to remove the newline character in the output result ('\r', '\r\n', \n'), the default is False, the newline character is not included, if it is True, the newline character is retained result: ['ab c', '', 'de fg', 'kl'] ['ab c\n', '\n', 'de fg\r', 'kl\r\n']
—————————str (string judgment True/False) ———————— Whether the
isalnum string contains only letters and numbers
test = "12aasd3" v = test.isalnum () print(v) test1 = "12aasd3@$%#" v1 = test1.isalnum() print(v1) result: True False
isalpha determines whether the string is pure English characters
test = "asaasd" v = test.isalpha () print(v) test1 = "12aasd3@$%#" v1 = test1.isalpha() print(v1) result: True False
isdigit determines whether the string is a pure number
test = "123123123" v = test.isdigit () print(v) test1 = "12312qweqw" v1 = test1.isdigit() print(v1) result: True False
isidentifier判断字符串是否是字母开头
print("name".isidentifier()) print("1True".isidentifier()) result: True False
islower determines whether the string is lowercase
test = "asdasda" v = test.islower() print(v) test1 = "ddwqdDSD" v1 = test1.islower() print(v1) result: True False
isnumeric judgment字符串是否只由数字组成。这种方法是只针对unicode对象。
test = "asjdhgjasd" test1 = "12123123" v = test.isnumeric() print(v) v1 = test1.isnumeric() print(v1) result: False True
Is isprintable printable, all that can be printed are true
print("My Name Is".isprintable())
isspace determines whether the string is a space
test = "123123" v = test.isspace() print(v) test1 = " " v1 = test1.isspace() print(v1) result: False True
istitle determines whether the first letter is uppercase
test = "Asdasd Asdas" v = test.istitle () print(v) test1 = "Asdasd aAasdasd" v1 = test1.isttitle() print(v1) result: True False
isupper determines whether all characters are uppercase
test = "asdasdasd" v = test.isupper() print(v) test1 = "SADSADSA" v1 = test1.isupper() print(v1) result: False True
—————————list (list) ————————
append add
test = ['a','b','c','d','e','f','g'] v = test.append("z") #Add a new object at the end of the list print(test) result: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'z']
clear clear
test = ['a','b','c','d','e','f','g'] v = test.clear() print(test) result: []
copy copy
test = ['a','b','c','d','e','f','g'] v = test.copy() print(test) print(v) result: ['a', 'b', 'c', 'd', 'e', 'f', 'g'] ['a', 'b', 'c', 'd', 'e', 'f', 'g']
index finds the corresponding subscript
test = ['a','b','c','d','e','f','g'] v = test.index("c") print(v) result: 2
insert insert
test = ['a','b','c','d','e','f','g'] v = test.insert(5,'z') #The first parameter to be changed is inserted in the table below, and the second parameter is the inserted value print(test) result: ['a', 'b', 'c', 'd', 'e', 'z', 'f', 'g']
pop removes an element from the list (default last element)
test = ['a','b','c','d','e','f','g'] v = test.pop () v2 = test.pop() print(v) print(v2) print(test)
remove removes the specified list element
test = ['a','b','c','d','e','f','g'] v = test.remove("c",) print(test) result: ['a', 'b', 'd', 'e', 'f', 'g']
reverse
test = ['a','b','c','d','e','f','g'] print(test) v = test.reverse() print(test) result: ['a', 'b', 'c', 'd', 'e', 'f', 'g'] ['g', 'f', 'e', 'd', 'c', 'b', 'a']
sort sort
test = ['a','b','f','c','e','d','g','1','7','3','6','2','4'] print(test) v = test.sort() print(test) result: ['a', 'b', 'f', 'c', 'e', 'd', 'g', '1', '7', '3', '6', '2', '4'] ['1', '2', '3', '4', '6', '7', 'a', 'b', 'c', 'd', 'e', 'f', 'g']
—————————tuple (tuple) ————————
count count
test = ('a','b','f','a','a','d','g','1','7','a','6','2','4') print(test) v = test.count('a') print(v) result: ('a', 'b', 'f', 'a', 'a', 'd', 'g', '1', '7', 'a', '6', '2', '4') 4
index to find the corresponding subscript
test = ('a','b','f','a','a','d','g','1','7','a','6','2','4') print(test) v = test.index('a',2,10) #2 is the starting position, 10 is the end position to search print(v) result: 3
—————————dict (dictionary) ————————
fromkeys #Create a key according to the sequence and formulate a uniform value.
dec = { "k1": 1, "k2": 2, "k3": 3, "k4": 4, } #Create a key based on the sequence and formulate a uniform value. v = dict.fromkeys(["k10",123,"9999"],123) print(v) result: {'9999': 123, 123: 123, 'k10': 123}
get #根据key获取值,key不存在时,可以指定默认值(None),也只可以指定返回值。
dic = { "k1": 1, "k2": 2, "k3": 3, "k4": 4, } v = dic.get('ke1',1111) #指定返回值 1111 print(v) v = dic.get('ke1') #默认返回值 print(v) 结果: 1111 None
items #取出所有的key,values
dic = { "k1": 1, "k2": 2, "k3": 3, "k4": 4, } for i,o in dic.items(): print(i,o) 结果: k2 2 k4 4 k1 1 k3 3
keys 取出所有的key
dic = { "k1": 1, "k2": 1, "k3": 1, "k4": 1, } for i in dic.keys(): #取出所有的key print(i) 结果: k1 k2 k4 k3
pop #删除并获取值
dic = { "k1": 1, "k2": 2, "k3": 3, "k4": 4, } #删除并获取值 v = dic.pop('k1',90) #当key不存在时,返回第二值90 print(dic,v) #打印删除values 结果: {'k4': 4, 'k3': 3, 'k2': 2} 1
popitem #随机删除一个值
dic = { "k1": 1, "k2": 2, "k3": 3, "k4": 4, } a,c = dic.popitem() #随机删除一个值 print(dic,a,c) #打印随机删除key,values. 结果: {'k3': 3, 'k2': 2} k4 4
setdefault #设置key,values,如果值存在,获取当前values, 如果值不存在,添加设置的值,key:values,
dic = { "k1": 1, "k2": 2, "k3": 3, "k4": 4, } v = dic.setdefault('k1','123') #如果值存在,获取当前values, print(dic,v) v = dic.setdefault('k1121','123') # 如果值不存在,添加设置的值,key:values, print(dic,v) 结果: {'k1': 1, 'k2': 2, 'k4': 4, 'k3': 3} 1 {'k1121': '123', 'k1': 1, 'k2': 2, 'k4': 4, 'k3': 3} 123
update #更新 update
dic = { "k1": 1, "k2": 2, "k3": 3, "k4": 4, } #更新 update dic.update({'k1':123,'k123':'dasdasd'}) print(dic) #另一种写法 dic.update(k1=3123,k5="qweqweasd") print(dic) 结果: {'k123': 'dasdasd', 'k1': 123, 'k4': 4, 'k2': 2, 'k3': 3} {'k123': 'dasdasd', 'k1': 3123, 'k4': 4, 'k2': 2, 'k3': 3, 'k5': 'qweqweasd'} 有值就替换掉,没有key,values,就添加上
values # 取出所有的values
dic = { "k1": 1, "k2": 2, "k3": 3, "k4": 4, } for i in dic.values(): # 取出所有的values print(i) 结果: 4 1 2 3
ps:本人新手整理资料,请各位大神勿喷,多多耐心指点!!!谢谢。