To judge the contents of a string, returning a Boolean type
#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" print (' Python '. EndsWith (' on ')) #判断字符串是否以on结尾, returns the Boolean type print (' Python '). StartsWith (' py ')) #判断字符串是否以py开头, returns the Boolean type print (' abc123 '. Isalnum ()) #判断字符串是否仅为字母和数字组成, returns the Boolean type print (' abc '. ISALPHA ()) # Returns the Boolean type print (' 0x7c '. Isdecimal ()) #判断是否为十进制, returns the Boolean type print (' 1.2 '. IsDigit ()) #判断是否为整数, returns the Boolean type print (' abc '). Isidentifier ()) #判断字符串是否为一个合法的变量名, returns the Boolean type print (' abc '. islower ()) #判断字符串是否全为小写字母, returning the Boolean type print (' abc '. isupper ()) # Determines whether the string is all uppercase, returns the Boolean type print (' 1.2 '. IsNumeric ()) #判断是否仅有数字, does not have a decimal point, returns the Boolean type print ('. Isspace ()) #判断是否为空格, returns the Boolean type print (' This is A Title '. Istitle ()) #判断字符串是否为标题格式, returns the Boolean type print (' abc '. isprintable ()) #判断是否能打印, returns a Boolean type
Segmentation of strings
#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" Print (' http://www.baidu.com '. Partition ('://')) #根据指定的分隔符对字符串进行分割 # Returns a tuple of $3, the first is the string to the left of the delimiter, the second is the delimiter itself, and the third is the string to the right of the delimiter # if the delimiter is leftmost, the first is empty, and if the delimiter is on the far right, the third is the empty print (' http://www.baidu.com '. Split ('://')) #根据指定的分隔符对字符串进行分割 # Returns a segmented list of strings, excluding the delimiter print (' Pyt\nhon '. Splitlines ()) #根据换行符进行分割
Run results
Formatting of strings
#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" info = ' My name is ' {name} ' and ' I age ' {age} ' Print (Info.format (name= ' Joh n ', age=23)) #字符串格式化print (Info.format_map ({' name ': ' Jack ', ' age ': ') ') #以字典格式把字符串格式化
Run results
Index operations on strings
#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" print (' Python '. Index (' o ')) #打印字符串所在位置的索引值, if there is no error print (' Python '). Index (' o ', 0,5)) #0为开始索引的索引值, 5 is the indexed value of the end index print (' Python '. Find (' t ')) #打印字符串所在位置的索引 if there is no index value of -1print (' Java '. RFind (' a ')) #从右往左找对应的字符串, if there is the same character, returns the index of the first-found character, the index or the left-to-right number
Run results
string-Case operations
#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" print (' Python '. Capitalize ()) #首字母大写print (' This is a title '. Title ()) #把字符 The string is converted to the title format print (' abc '. LOWER ()) #把字符串转为全小写print (' abc '. UPPER ()) #把字符串转为全大写print (' abc '. swapcase ()) #把字符串中的大写转化为小写, Lowercase converted to uppercase
Run results
Padding of strings
#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" print (' Python '. Center (') ') #把字符串居中, with '-' padding to 20 characters in print (' Python '. Ljust (+, ' + ')) #字符串在左边, to the right with ' + ' to fill the string to 25 characters print (' Python '. Rjust (' # ')) #字符串在右边, left with ' # ' to fill the string to 30 characters print (' abc ' . Zfill) #用0把字符串填充到20个字符数name = ' John \tand Jack ' Print (Name.expandtabs (tabsize=20)) #把table键填充到第20位数为止
Run results
Calculation of strings
#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" print (' Python '. Count (' t ')) #计算字母t的个数
Run results
Remove whitespace and newline characters from a string
#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" Print (' abc '. strip ()) #删除字符串两边的空格和回车print (' abc '. RSTRIP ()) #删除字符串右边的空格和 Enter print (' abc '. LSTRIP ()) #删除字符串左边的空格和回车
Run results
Substitution of strings
#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" s = Str.maketrans (' abcdef ', ' 123456 ') #前后个数要对应print (' Java and Perl '). Trans Late (s)) #字母会被对应的数字替换print (' Python '. replace (' P ', ' P ')) #替换字符串
Run results
Connection of strings
#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" print (' + '. Join ([' 1 ', ' 2 ', ' 3 ')) #将序列中的元素以指定的字符连接生成一个新的字符串
Run results
python-string manipulation