Individual learning summary notes if there are deficiencies and errors Welcome to teach Thank you
str string
For example:
name = "adam"
name1 = "Zhou Dafu"
There are some common commands in str
1, capitalize * 2, casefold and lower * 3, center * 4, count *
5. endswith and startswith * 6, expandtabs * 7, find * 8, format * 9, format_map
10,isalnum 11,isalpha 12,isdecimal,isdigit,isnumeric 13,isidentifier
14,islower,lower,isupper,upper 15,isprintable 16,isspace 17,istitle,title
18,join 19,ljus,rjust,center,zfill 20,lstrip ,rstrip,strip 21,translate,maketrans
22,partition,rpartition,split,rsplit,splitlines() ,splitlines(True) ,splitlines(Fales)
23,swapcase 24,replace
PS: Conditional parameter configuration format for command statements
center(self,width,fillchar= None)
self:can be ignored directly
width:Must have parameters
fillchar= None: You don't need to set parameters separately
Anything with a parameter= None can be understood as not having to set this parameter separately
1. capitalize
name = "adam" v = name.capitalize() #Uppercase the first letter of a string within name print(type(v),v)
2. casefold and lower, all lowercase
name = "aDam" v = name.casefold() #The string in name becomes lowercase (it can handle case and letter in different languages) print(type(v),v) name = "aDam" v2 = name.lower() #The string in name becomes lowercase (it only works in English) print(type(v2),v2)
3. center sets the string length and fills the blank space in the content play and how to set the default fill space for the filled symbol
name = "adam" v = name.center(25,"+") #Add a plus sign to the front and back of the name assignment string so that the entire string takes up the assignment string of name before a total of eight characters and is assigned to v print(type(v),v)
4. count finds the number of occurrences of a subsequence or character in a string and can set the starting number of digits to look for
name = "adam" v = name.count('a',2,4) #Calculate how many times a occurs in the name assignment string. If you don't write (, 2, 4), the default is to look for it head-in-place. Writing is to look for it from 2 to 4 digits print (v)
5. endswith and startswith
endswith determines what the character at the end can start with
startswith determines what the starting character is and what it can set the starting position Note that the starting position of the setting needs to be moved forward by one unit
name = "adam" v = name.endswith('d',1,2) #Determines whether the name assignment string from the first character to the end of the second character is d v1 = name.startswith('a',0,2)#Determines whether the string assigned by name starts with the letter a from the 0th to the 2nd position print (v) print (v1)
6. expandtabs replace the leading \t character digits with spaces to the x-bit, if the leading \t character digits equal to x, with X spaces directly \t If the leading \t character digits are greater than x, then the remaining x spaces are filled at the end of the preceding character (the output will no longer show \t)
test = '12345\t6789' v = test.expandtabs(8) v1 = test.expandtabs(3) v2 = test.expandtabs(5) print(v) print(v1) print(v2) #Application examples: test = 'Full name\t Gender\t height\n Zhang San\t male\t180\n Zhang San\t male\t180\n Zhang San\t male\t180' v = test.expandtabs(10) print(v)
7. find finds the first subsequence from beginning to end to get the number of characters in front of it. You can also specify the search range, which sets the starting number of logical relation bits <=the searched range <the ending position.
name = "adamadamadam" #Create variable name and assign adamadamadam v = name.find('ma',6,9) #Set search ma subsequence from 6 to 8 bits print (v) #Show the value of v #The result is:7
PS: A BIT-1 is assigned when find cannot find the target subsequence, while another find command index will directly program error when it cannot find the target subsequence, so the find command is recommended.
8. format, replacing placeholders in a string with the specified value "Placeholders in braces"
name = "adam" #Create a variable name and assign it to adam xingBie = "male" #Create the variable xingBie and assign it to Men xianShi = "I am {name1},{xingBie1}" #Create variable xianShi and assign values to "I am {name1},{xingBie1}" print(xianShi) #Show xianShi #Results: I am {name1},{xingBie1} v = xianShi.format(name1 = name ,xingBie1 = xingBie) #Replace name1 and xingBie1 in the xianShi variable with variables name and xingBie and assign them to v print(v) #Show v #Result: I am adam, male
Form's other writing format, which uses placeholders for numbers and assigns them one by one by default. To create placeholders, you need to start with 0.
name = "adam" #Create a variable name and assign it to adam xingBie = "male" #Create the variable xingBie and assign it to Men xianShi = "I am {0},{1}" #Create variable xianShi and assign values to "I am {0},{1}" print(xianShi) #Show xianShi #Results: I am {0},{1} v1 = xianShi.format(name ,xingBie) #Replace values 0 and 1 in xianShi with values of variable name and xingBie and assign them to v1 print(v1) #Show v1 #Result: I am adam, male
9. format_map (dictionary)...
name = "adam" #Create a variable name and assign it to adam xingBie = "male" #Create the variable xingBie and assign it to Men xianShi = "I am {name1},{xingBie1}" #Create variable xianShi and assign values to "I am {name1},{xingBie1}" print(xianShi) #Show xianShi v = xianShi.format(name1 = name ,xingBie1 = xingBie) #Replace name1 and xingBie1 in the xianShi variable with variables name and xingBie and assign them to v print(v) #Show v #Result: I am adam, male v1 = xianShi.format_map({'name1':name,'xingBie1':xingBie}) #Assign a value to v1 using the format_map format print(v1) #Show v1 #Result: I am adam, male
10. isalnum determines if there are only letters or numbers in the string and outputs a Boolean value
name = 'adam123' #Assigning adam to variable name name1 = 'adam+' #Assign adam+to name1 name2 = 'adam' name3 = '123' v = name.isalnum() #Determine if the name contains only letters or numbers and assign a Boolean value to v v1 = name1.isalnum() #Determine if name1 contains only letters or numbers and assign a Boolean value to v1 v2 = name2.isalnum() v3 = name3.isalnum() print(v) print(v1) print(v2) print(v3)
11. isalpha determines if a string contains only letters or Chinese characters and outputs a Boolean value
name = 'adam123' #Assign adam123 to variable name name1 = 'adam+' #Assign adam+to name1 name2 = 'adam' #Assign adam to name2 name3 = 'Zhang San' #Assign name 3 to Zhang San v = name.isalpha() v1 = name1.isalpha() v2 = name2.isalpha() v3 = name3.isalpha() print(v) print(v1) print(v2) print(v3)
12. isdecimal, isdigit, isnumeric. They are used to determine if a string is merely a numeric Boolean
isdecimal: only the Arabic numerals can be judged to be true (this comparison is recommended)
isdigit: Arabic numerals and number of symbols "123 and 3" are true
isnumeric: Arabic numerals, symbolic numerals, Roman numerals and Chinese characters are all true "123, 3, III, 4"
test1 = '123' #assignment test2 = '1a' #This can also be a 16-digit number test3 = '②' #Symbol Number 2 test4 = 'Ⅲ' #Roman numeral 3, this is a character test5 = 'four' #Chinese Character Number Four vecimal1 = test1.isdecimal() #Determine whether a string is a number vecimal2 = test2.isdecimal() vecimal3 = test3.isdecimal() vecimal4 = test4.isdecimal() vecimal5 = test5.isdecimal() vigit1 = test1.isdigit() #Determine whether a string is a number vigit2 = test2.isdigit() vigit3 = test3.isdigit() vigit4 = test4.isdigit() vigit5 = test5.isdigit() vnumeric1 = test1.isnumeric() vnumeric2 = test2.isnumeric() vnumeric3 = test3.isnumeric() vnumeric4 = test4.isnumeric() vnumeric5 = test5.isnumeric() print(vecimal1, vecimal2, vecimal3, vecimal4, vecimal5) #Print the result of isdecimal judgment print(vigit1, vigit2, vigit3, vigit4, vigit5) #Print the result of isdigit judgment print(vnumeric1, vnumeric2, vnumeric3, vnumeric4, vnumeric5)
13. isidentifier tests whether strings are composed of alphanumeric underscores and do not begin with letters or underscores
PS: This command will not be false because the string is a python key
print( "if".isidentifier() ) print( "def".isidentifier() ) print( "class".isidentifier() ) print( "_a".isidentifier() ) print( "China 123 a".isidentifier() ) print( "123".isidentifier() ) print( "3a".isidentifier() ) print( "".isidentifier() )
14,islower,lower,isupper,upper
islower determines whether strings are all lowercase
lower lowers all characters in a string
isupper determines whether strings are all uppercase
upper capitalizes all characters in a string
name = 'Adam' name1 = 'ADAM' name2 = 'adaM' name3 = 'aDam' v = name.islower() v1 = name1.islower() v2 = name2.islower() v3 = name3.islower() print('Adam',v,'ADAM',v1,'adaM',v2,'aDam',v3) x = name.lower() x1 = name1.lower() x2 = name2.lower() x3 = name3.lower() print('Adam',x,'ADAM',x1,'adaM',x2,'aDam',x3) n = name.isupper() n1 = name1.isupper() n2 = name2.isupper() n3 = name3.isupper() print('Adam',n,'ADAM',n1,'adaM',n2,'aDam',n3) k = name.upper() k1 = name1.upper() k2 = name2.upper() k3 = name3.upper() print('Adam',k,'ADAM',k1,'adaM',k2,'aDam',k3)
15. isprintable determines whether a string contains invisible or unprintable characters such as \n t
print('\tabc'.isprintable()) print('abc\n'.isprintable()) print('\tabc\n'.isprintable()) print('abc'.isprintable())
16. isspace determines if all strings are spaces
test = ' ' test1 = ' a' test2 = ' a ' test3 = '' v = test.isspace() v1 = test1.isspace() v2 = test2.isspace() v3 = test3.isspace() print(v,v1,v2,v3) #True False False False
17,istitle,title
istitle determines whether it is a title (all words begin with uppercase letters)
Title converts a string to a title, that is, converts the first letters of all letters to uppercase
test = 'wo shi zhang san , wo ba sui le ' v = test.istitle() print(v) #False v1 = test.title() print(v1) #Wo Shi Zhang San , Wo Ba Sui Le v2 = v1.istitle() print(v2) #True
18. join adds a set interval between characters. When there is only one character in the variable value, it does nothing. You can set the added symbol
test = 'I'm Zhang San. I'm eight years old' test1 = 'I' print(test) #I'm Zhang San. I'm eight years old x = ' ' v = x.join(test) print(v) #I'm Zhang San. I'm eight years old v1 = x.join(test1) print(v1) #I
19,ljus,rjust,center,zfill
ljust string left aligned not enough digits with set character completion
Right alignment of rjust strings not enough digits to complete with set characters
center string plays are filled with characters set to align not enough digits, if only one character is needed, to the left of the string
zfill can only fill 0 to the left of the string
name = 'adam' v = name.ljust(6,'*') #Text left-aligned not to 6 digits with *complement v1 = name.rjust(6,'*') #Right text alignment not to 6 digits with *complement v2 = name.center(6,'*') #Less than 6 digits in script play are complemented by * v3 = name.zfill(6) #Right text alignment not to 6 digits with *complement print(v,v1,v2,v3) #adam** **adam *adam* 00adam
20,lstrip ,rstrip,strip
lstrip removes spaces from left-to-right matching until it encounters the mismatched character
rstrip removes spaces from right-to-left matches until it encounters the mismatched character
strip
PS: They can also remove functions like/t/n (by default) or specify to remove any characters, the matching principle being the most preferred matching principle
name = ' ad am ' v = name.lstrip() #Remove the left space until you encounter other characters v1 = name.rstrip() #Remove the right space until you encounter other characters v2 = name.strip() #Remove whitespace on both sides until you encounter other characters print(v) #ad am print(v1) # ad am print(v2) #ad am
21,translate,maketrans
translate: Perform mapping transformation
maketrans: Create a mapping conversion relationship by writing to the original character table, then to the character table to be converted, or by defining a character table that needs to be deleted
yuanZiFu = 'abcde' #Define the original matching character bianGengZiFu = '12345' #Define characters that change after matching shanChuZiFu = 'fghij' #Define original matching deleted characters zhuanHuanBiao = str.maketrans(yuanZiFu,bianGengZiFu) #Create a Character Mapping Conversion Table (matching yuanZiFu to bianGengZiFu) zhuanHuanShanChuBiao = str.maketrans(yuanZiFu,bianGengZiFu,shanChuZiFu) #Create a Character Mapping Conversion Deletion Table (matching yuanZiFu to bianGengZiFu to delete shanChuZiFu) yuanShiZiFuChuan = 'a1b2c3d4e5fagbhcidje' #Create original string print(yuanShiZiFuChuan.translate(zhuanHuanBiao)) #1122334455f1g2h3i4j5 print(yuanShiZiFuChuan.translate(zhuanHuanShanChuBiao)) #112233445512345
22,partition,rpartition,split,rsplit,splitlines() ,splitlines(True) ,splitlines(Fales)
partition divides a string into three parts, looking for the set separator symbol from the left to the right of the string, and the separator symbol is separately divided into the middle part
rpartition divides a string into three parts, looking for the set separator symbol from the right to the left of the string, and the separator symbol is separately divided into the middle part
ps partition and rpartition need to set the splitter
Split allows you to specify the number of blocks to be split Look left to right for the split symbol All split symbols matched by default are split and the split symbol does not appear in the result
rsplit can specify the number of blocks to split. Look left to right for the split symbol. By default, all matching split symbols are split and the split symbol does not appear in the result
ps: The separator defaults to all empty characters, including spaces, line breaks (\n), tabs (\t), and so on.
splitlines() are split by default according to the line break symbol\n and will not retain the line break symbol
splitlines(True) Split by the line break symbol\n and keep the line break symbol Line break symbol appearing on the right side of the split module splitlines(Fales) Split by the line break symbol\n without retaining the line break symbol
x1 = '123\n45\t123\n45\t123\n45\t123\n45' print(x1.partition('5')) #('123\n4', '5', '\t123\n45\t123\n45\t123\n45') print(x1.rpartition('5')) #('123\n45\t123\n45\t123\n45\t123\n4', '5', '') print(x1.split('\n',3)) #['123', '45\t123', '45\t123', '45\t123\n45'] print(x1.rsplit('\t',2)) #['123\n45\t123\n45', '123\n45', '123\n45'] print(x1.splitlines()) #['123', '45\t123', '45\t123', '45\t123', '45'] print(x1.splitlines(True)) #['123\n', '45\t123\n', '45\t123\n', '45\t123\n', '45'] print(x1.splitlines(False)) #['123', '45\t123', '45\t123', '45\t123', '45']
23. swapcase case conversion, converting all the case inside the string
name = 'Adam' v = name.swapcase() print(v) #aDAM
24. Replace substitution, search left to right for matching subsequences, and then replace them all. You can set several substitutions, ("subsequences in the original variable", "substituted subsequences", substituted several subsequences)
name = 'adamadamadam' v = name.replace('da','yyy') print(v) #ayyymayyymayyym
PS: Matching principle longest common subsequence The longest identical subsequence in two or more strings