One, string slices
# slices can also fit into the string tittle=' today's hair apple ' for in Enumerate (tittle): # enumerate can loop subscript and value at the same time Print ('%s:%s' % (i,t))
Second, non-empty is true
# non-empty is true Print (1>2)
Three, String common operation
#mutable variables: list dic#immutable variable tuple str#The remove can not be removed when the list is recycled, which causes the subscript to change#Example:#can not use l2=l such memory address is the same, l change, L2 will also changel=[1,1,2,3,4,5,6,7,8]l2=l[:] forIinchL2:ifi%2!=0:l.remove (i)Print(l) name='besttest 123\n'new_name=name.strip ()#by default, both spaces and line breaks are removedNew_name2=name.lstrip ()#by default, the left space and line breaks are removedNew_name3=name.rstrip ()#RightNew_name4=name.count ('T')#find the number of occurrences of a character in a stringNew_name5=name.center (50,'a')#50 a,name in the middle of the placeIndex=name.find ('s')#find the string subscript, can not find return-1, the same return to the first subscriptIndex2=name.index ('s')#find the string subscript, can not find the errorName.upper ()#Turn all letters into uppercaseName.lower ()#Turn all letters into lowercasefile_name='A.xls'Print(File_name.endswith ('. xls'))#determines whether a string ends in XLSF='{Name} welcome'Print(F.format (name='Suki'))#string Formattingd={'name':'Suki'," Age":' -'}Print(D.format_map (d))#string formatting, passed in is a dictionarySql='SELECT * from user'New_sql=sql.replace ('Select','Update')#string Substitution#The most important methodst='a,b,c,d,e,f,g'St.split (',')#remove comma, string into list#List Variable stringsl=['a','b','C','D','e']','. Join (SL)
Python Learning Note 2-string manipulation