I. Duplicate OUTPUT string
Print (' Hello ' *20) #输出20个hello
Two. Getting characters in a string by index
Print (' HelloWorld ' [2:]) #输出lloworld
Three. Keyword in
Print (' ll ' in ' Hello ') #输出True
Four. Formatted output
Print (' Darling,i love your ') print ('%s,i love You '% ' Darling ')
Five. Connection of strings
A= ' 123 ' b= ' abc ' d= ' c= '. Join ([a,b,d]) print (c) #输出123abc44c = ' * '. Join ([a,b,d]) print (c) #输出123 *abc*44
Six. Built-in methods for strings
Str= ' Darling,i Love You ' Print (Str.count (' L ')) # statistic element ' L ' number of print (Str.capitalize ()) # only initial capital print (Str.center (50 , ' # ') # Center ############## #Darling, I love you############### #print (Str.endswith (' You ')) # Determine whether to end with a certain content print ( Str.startswith (' darling ') # determines whether to start with something, where output falseprint (str.find (' I ')) # finds the first element and returns the index value if there is no output for that element -1print ( Str.index (' a ')) #查找到第一个元素, and returns the index value, if there is no element then the error print (' {name} is {age} '. Format (name= ' Sfencs ', age=19)) # Another way to format the output is to Sfencs is 19print (' {name} ' (' {') ', ' {' name ': ' {') ', ' format_map ', ' Sfencs ', ' [age ':] ') print (' Dar\tling,i love You '). Expandtabs (tabsize=20)) #制表符的长度为20print (' ASD ' isalnum ()) #检测字符串是否由字母和数字组成print (' 12632178 '. Isdecimal ()) # Checks whether the string contains only the decimal character print (' 1269999 '. IsNumeric ()) #检测字符串是否只由数字组成print (' abc '. isidentifier ()) #判断是否满足标识符定义规则. Can only start with letters or underscores, and cannot contain any characters except numbers, letters, and underscores. Print (' abc '. islower ()) #检测字符串是否全由小写字母组成print (' abc '. isupper ()) #检测字符串是否全由大写字母组成print (' E '. isspace ()) # Detects whether a string is made up of only spaces print (' My title '. Istitle ()) #检测字符串中所有的单词拼写首字母是否为大写, and the other letters are lowercase print (' My title '. Lower ()) #转换字符串中所有大写字符为小Write Print (' My title '. Upper ()) #转换字符串中所有小写字符为大写print (' My title '. Swapcase ()) #对字符串的大小写字母进行转换print (' My title '. Ljust (10, ' * ')) #返回一个原字符串左对齐 and fills a new string of the specified length with a space My title**print (' My TItle '. Rjust (' * ')) #返回一个原字符串右对齐 and fills a new string of the specified length with a space **my Titleprint (' \tmy tltle\n '. Strip ()) #用于移除字符串头尾指定的字符 (the default is a space or newline character) or the character sequence print (' \tmy tltle\n '. Lstrip ()) # Used to truncate the left-hand space of a string or specify the character print (' \tmy tltle\n '. Rstrip ()) #用于截掉字符串右边的空格或指定字符print (' My title ' title '. Replace (' title ', ' new ', 1)) Replace the #把字符串中的 old string with the new one, and if you specify the third parameter max, the replacement does not exceed Max times. Print (' My title title '. RFind (' t ')) #从右向左寻找第一个t的索引print (' My title title '. Split (' I ', 1)) #通过指定分隔符对字符串进行切片, the number parameter is the number of splits, Do not fill in the full split print (' My title title '. Title ()) #返回 ' title ' string, that is, all words start with uppercase, the remaining letters are lowercase
Python Basic Learning 6----string manipulation