One: data types are converted to each other: Numbers and strings: int ----> str str(int) str ----> int int(str) can only be a string of numbers number and bool int ----> bool 0 Fasle non-zero True bool ----> True T ---> 1 F --->0 String and bool value str ---> bool non-empty True empty is False bool ----> str str(True) str(False) ''' i = bool(3) # print(i) # i1 = bool(0) # print(i1) # i = int(True) # print(i) # i = int(False) # print(i)
Two: the code runs faster
Three: Slicing and Indexing
s='alex wusir ritian'
Output the results separately:
You can also cut in the opposite direction and increase the step length
s1 = s[0:3:2] print(s1) s2 = s[-1:-6:-2] print(s2)
result:
Four: String usage
Common methods for strings s = 'alex wuSir' *capitalize() first letter is capitalized, other letters are lowercase print(s.capitalize()) *swapcase() case inversion print(s.swapcase()) Non-letter-separated parts, first letter uppercase, other lowercase s = 'alex wuSir1taibai*ritian' print(s.title()) s = 'alexawuSir' ***upper all caps ***lower all lowercase print(s.upper()) print(s.lower()) code = 'aeDd' your_code = input('Please enter the verification code:') if your_code.upper() == code.upper(): print('input is correct') else :print('Please re-enter') * Centered by what, the padding is empty by default print(s.center(20)) print(s.center(20,'*')) s = 'al\tex wuSir' print(s.expandtabs()) ***find() finds the index through the element, you can find it as a whole, you can slice it, and return -1 if it is not found index() finds the index through the element, which can be found as a whole, can be sliced, and an error will be reported if it is not found print(s.find('a'),type(s.find('a'))) print(s.find('alex'),type(s.find('a'))) print(s.find('a')) print(s.find('a',1,5)) print(s.find('L')) print(s.index('L')) s = 'alex wusir' *** startswith endswith print(s.startswith('a')) print(s.startswith('al')) print(s.startswith('w',5)) print(s.startswith('W',5)) print('adfads\n','fdsa') print(s) s = '\talex wusir\n' s1 = 'alalelllllllxwusirbl' *****strip removes spaces, newlines, tab keys, etc. print(s.strip()) print(s.lstrip()) print(s.rstrip()) print(s1.strip('lab')) name = input('Please enter the name:').strip() if name == 'alex': print('somebody') else :print('Please re-enter') s = 'alex;wusir;ritian' s1 = 'alexalaria' ******split str --->list method print(s.split(';')) print(s.split(';')) print(s1.split('a',1)) replace ****** s1 = 'Sister and brother come together old boy old boy old boy' s2 = s1.replace('old', 'small', 1) print(s2) name='jinxin123' print(name.isalnum()) #The string consists of letters or numbers print(name.isalpha()) #The string consists only of letters print(name.isdigit()) #The string consists only of numbers
Five: format usage:
format The first s = 'My name is {}, this year{}, height{}'.format('Jin Xin',21,175) print(s) the second s = 'My name is {0}, this year is {1}, my height is {2}, my name is still {0}'.format('Jin Xin',21,175) print(s) the third s = 'My name is {name}, this year {age}, height {high}'.format(name = 'Jin Xin',high=175,age=21) print(s) ### public methods count counts the number of times an element appears s = 'alexaaaaa wusir' print(s.count('a')) s = 'alex' print (len (s))