String correlation method
1. Important methods
a,join
Format: string. join (sequence) -- merge the elements in the sequence into a new string through the specified string (all elements in the sequence must be strings)
eg:
list1 = ['name', 'age', 'sex'] str1 = ''.join(list1) print(str1) # nameagesex str2 = '+'.join(list1) print(str2) # name+age+sex num = [100, 200, 300, 400] str1 = '-'.join([str(x) for x in num]) print(str1) # 100-200-300-400
Exercise: given a list, connect all strings in the list with '= =' to form a new string
list1 = [100, 'abc', True, 'Hello', 'hello', 12.9] # 'abc = = hello = = hello' str1 = '=='.join([i for i in list1 if type(i) == str]) print(str1)
b,split
Format:
String 1. Split (string 2) -- cut all strings 2 in string 1 as cutting points
String 1. Split (string 2, N) -- cut the first N string 2 in string 1 as the cutting point
If string 1 does not contain string 2, the original string is returned
eg:
print(str1.split('see')) # ['you ', ' ', ',one day day!'] print(str1.split('y')) # ['', 'ou see see,one da', ' da', '!']
c,replace
Format:
String 1. Replace (string 2, string 3) - replaces all string 2 in string 1 with string 3
String 1. Replace (string 2, string 3, N) - replaces the first N string 2 in string 1 with string 3
eg:
print(str1.replace('see', 'see')) # you see, one day! print(str1.replace('se', 'see', 1)) # you see, one day!
2. General method
a,center,rjust,ljust,zfill
Format:
String. Center (length, padding character): the original string is in the middle
String. Rjust (length, padding character): the padding character is on the right
String. Ljust (length, padding character): the padding character is on the left
String. Zfill (length): equivalent to - string. Rjust (length '0')
When the length is less than the length of the original string, the original string is printed
eg:
str1 = 'adfg' print(str1.center(8, '1')) # '11adfg11' print(str1.center(9, '1')) # '111adfg11' print(str1.rjust(9, '1')) # 11111adfg print(str1.ljust(9, '1')) # adfg11111 print(str1.zfill(9)) # 00000adfg print(str1.rjust(2, '1')) # adfg
b,count
Format:
String 1. Count (string 2) - counts the number of occurrences of string 2 in string 1
String 1. Count (string 2, start subscript, end subscript) - counts the number of string 2 in the start subscript and end subscript of string 1
eg:
str1 = 'you see see,one day day!' print(str1.count('e')) # 5 print(str1.count('e', 6, 11)) # 3 print(str1.count('e', 6)) # 4
c,find,index,rfind,rindex
Format:
String 1.find (string 2) -- get the position where string 2 appears for the first time in string 1 ----- if the string does not exist, return - 1
String 1.index (string 2) --- get the first occurrence position of string 2 in string 1 ------ there is no string and an error is reported
String 1.find (string 2, start subscript, end subscript)
String 1.index (string 2, start subscript, end subscript)
String 1.rfind (string 2) - find from back to front
String 1.rindex (string 2) - find from back to front
eg:
str1 = 'you see see,one day day!' print(str1.find('see')) # 4 print(str1.index('see')) # 4 print(str1.find('aaa')) # -1 # print(str1.index('aaa')) # report errors print(str1.rfind('see'))
d. Judgment method
Format:
String. isalnum() - judge whether the string contains only numbers, letters and Chinese
String. isalpha() - judge whether the string contains only letters and Chinese
String. isdigit() - judge whether the string is a numeric character of pure numeric string 0 ~ 9
String. isnumeric() - judge whether the string is a pure numeric string. The meaning of each character is a number (one character)
String. isspace() - judge whether the string is a pure blank string
String. isupper() - judge whether all letters in the string are uppercase letters - it is mostly used to judge a single character
String. islower() - judge whether all letters in the string are lowercase letters - mostly used to judge a single character
eg:
print('1234'.isdigit()) # True print('①12 hundred'.isnumeric()) # True print('A'.isupper()) # True print('a'.islower()) # True
e,strip,lstrip,rstrip
Format:
String. strip() -- remove the white space before and after the string
String. lstrip() -- remove the blank space on the left of the string
String. rstrip() -- remove the blank space on the right of the string
f,maketrans,translate
Use with: str.maketrans(String 1,String 2) -Create a corresponding table for string 1 and string 2 character string.translate(surface)
eg:
str1 = 'you see see,one day day!' table1 = str.maketrans('oed', '$#&') str2 = str1.translate(table1) print(str2) # y$u s## s##,$n# &ay &ay!
String formatting
a. String format placeholder
Format: String% with format placeholder (data1, data2,...)
%s -- string placeholder. You can use any data that can be converted into a string -- Universal placeholder
%d -- integer placeholder
%f -- floating point placeholder ----%. Nf -- keep N decimal places ---- can correspond to any numeric data
%c -- character station character -- data occupying 1 character -- can be any character coding value
Note: the number of data should correspond to the position and number of placeholders
When there is only one data, parentheses can be omitted
eg:
a = 'Xiao Song' b = 18 c = 22.33 str1 = a + 'this year' + str(b) + 'year,' + 'Consumption today' + str(c) + 'element' print(str1) str2 = '%s this year%d Years old, consumption today%.2f element' % (a, b, c) print(str2) str4 = '%s this year%s Years old, consumption today%s element' % (a, b, c) print(str4) str1 = 's:%c' % 65 print(str1)
b. Supplementary r grammar
You can add R or R to the front of the string to make the function of escaping characters disappear
eg:
str1 = '\tcavd\t123\n@#$' print(str1) str1 = r'\tcavd\t123\n@#$' # It is equivalent to adding before the escape character\ print(str1) # \tcavd\t123\n@#$
c,f-string
Format: add f or F at the front of the string to provide data through {} in the string. Any resulting expression can be in {}
eg:
a = 'Xiao Song' b = 18 c = 22.33 str3 = f'{a}this year{b}Years old, consumption today{c if c < 10 else 0}element' print(str3) # Xiao Song is 18 years old and spends 0 yuan today
Add parameter: {expression providing data: parameter}
a. : Nf - keep N decimal places
b. :, --- separate the three digits of data with commas
c. :,. Nf ---- keep N decimal places when using commas
d. :. N% ----- convert decimal to percentage and keep N decimal
e. : x > N,: x < n ------ x fills in characters (if there is no X, fill in spaces), n is the len gt h, > fills in the front, < fills in the back
eg:
money = 15335 str1 = f'a monthly salary{money:.2f}' print(str1) num1 = 12000000 str1 = f'{num1:,.2f}' print(str1) rate = 0.78 print(f'Proportion:{rate:.2%}') num1=123 num2=34221 print(f'Xiao Ming's salary:{num1:<8} Age: 18') print(f'Xiaohong salary:{num2:<8} Age: 18')