1) The string of the slice and the index
s = 'ABCDLSESRF' #index s1 = s[0] print(s1) #A s2 = s[2] print(s2) #C s3 = p [-1] print(s3) #F s4 = s [-2] print(s4) #R #ABCD slice: care regardless tail s5 = s[0:4] print(s5) #ABCD s6 = s[0:-1] print(s6) #ABCDLSESRF # S7 = s [:] s6 same # S8 = s [0:] Function print (S7, S8) s9 = s [0: 0] # indicates not error-free s = 'ABCDLSESRF' # s [First: Last: step] s10 = s [0: 5: 2] print(s10) #ACL s11 = s[4:0:-1] print(s11) #LDCB s12 = s[3::-1] print(s12) #DCBA s13 = s[3::-2] print(s13) #DB s = 'ABCDLSESRF' s14 = s[-1::-1] print(s14) #FRSESLDCBA s15 = s[::-1] print(s15) #FRSESLDCBA
2) Operation of the string
1. capitalized .capitalize ()
s = 'alexWUsir' s1 = s.capitalize() print(s1)
2. ALL CAPS .upper (), all lowercase .lower ()
s = 'alexWUsir' s1 = s.upper() s2 = s.lower print(s1) print(s2)
s_str= 'acEQ1' you_input = input ( 'Enter the security code, case insensitive') if s_str.upper() == you_input.upper(): print ( 'Input success') else: print ( 'Please re-enter')
3. Flip case .swapcase ()
s3 = s.swapcase () print(s3)
4. Each separated (special characters or numbers) capitalized words .tittle ()
s = 'alex*egon-wusir' s4 = s.title() print(s4) s = 'fade,crazy*w4rri0r_songsong node_3' s4 = s.title() print(s4)
The center, a blank is filled .center ()
s = 'alexWUsir' s5 = s.center(20,'~') print(s5)
6. Remove / t replaced with spaces .expandtabs ()
s = 'alex \ survivors' s6 = s.expandtabs() print(s6)
7. .len string length ()
s = 'alex \ survivors' l = len (s) print(l)
8. begins with what .starwish, to what end .endwish
s = 'alexWUsir' s7 =s.startswith('alex') s71 = s.startswith('e',2,5) print (s71, s7)
9. By looking element index .find () .index
find find indexed by elements not found -1
index looking index by elements not found error
s = 'alexWUsir' s8 = s.find('A') s81 = s.index('A') print(s81s8)
10. Remove the space before and after the default .strip () .lstrip from left to right to delete delete .rstrip
s = 'alexWUsir%' s9 = s.strip('%') print (s9) s = ' *a%lexWUsi* r%' s91 = s.strip(' %*') print(s91)
11. .count find out the number of an element of character ()
s = 'alexaa wusirl' s10 = s.count('al') print(s10)
12. A cutting string list .split ()
s = ';alex;wusir;taibai' l = s.split('a') print(l)
13. .format output format ()
Three kinds of games are played formatted output #format s = 'My name is {}, {year}, {} hobby, to tell you my name is {}'. format ( 'Bai', 36, 'girl', 'Bai') print(s) name = input ( 'Please enter the name:') s = 'My name is {0}, {1} this year, hobbies, {2}, to tell you my name is {0}'. format (name, 36, 'girl') print(s) name = input ( 'Please enter the name:') s = 'My name is {name}, this {age}, hobby {hobby}, to tell you my name is {name}'. format (age = 18, name = name, hobby = 'girl') print(s)
14. Alternatively .replace string elements ()
s = 'to look at neighbors complex master good neighborhood law sprinkled see clients' s11 = s.replace ( 'neighborhood', 'Wang', 1) print(s11)