1、string.capitalize()
把字符串的第一个字符大写。
[python] view plain copy >>> myString = "hello world, hello everyone!" >>> myString.capitalize() 'Hello world, hello everyone!'2、string.center(width)
返回一个原字符串居中,并使用空格填充至长度width的新字符串。可以改变填充使用的字符。
[python] view plain copy >>> myString.center(40) ' hello world, hello everyone! ' >>> myString.center(40, '*') '******hello world, hello everyone!******'
3、string.count(str, beg=0, end=len(string))
返回 str 在 string 里面出现的次数,如果 beg 或者 end 指定则返回指定范围内 str 出现的次数。
[python] view plain copy >>> myString.count('hello') 2 >>> myString.count('hello', 0, 10) 14、string.find(str, bg=0, end=len(string))
find方法可以检测str是否包含在string中,如果bg和end制定范围,则检查是否包含在指定范围内,如果是则返回开始的索引值,否则返回-1。
[python] view plain copy >>> myString.find('world') 6 >>> myString.find('am') -15、string.rfind(str, beg=0,end=len(string)) 类似于 find()函数,不过是从右边开始查找。返回str左边开始的索引值。
[python] view plain copy >>> myString.rfind('world') 6 >>> myString.rfind('hello') 13 #右边的hello的索引值6、string.index(str, beg=0,end=len(string)) 和find()方法一样,但是如果str不在string中会报一个异常。
7、string.rindex(str, beg=0,end=len(string)) 和index()一样,但是是从右开始。
[python] view plain copy >>> myString.index('world') 6 >>> myString.index('am') Traceback (most recent call last): File "<pyshell#28>", line 1, in <module> myString.index('am') ValueError: substring not found8、string.islower()
如果 string 中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是小写,则返回 True,否则返回 False。
9、string.isupper()
如果 string 中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是大写,则返回 True,否则返回 False。
[python] view plain copy >>> myString.islower() True >>> myString.isupper() False10、string.isnumeric() 如果 string 中只包含数字字符,则返回 True,否则返回 False。
11、string.join(seq)
Merges (concatenates) 以string 作为分隔符,将 seq 中所有的元素(的字符串表示)合并为一个新的字符串。
[python] view plain copy >>> myString = "+" >>> seq = ['8', '9'] >>> myString.join(seq) '8+9'12、string.ljust(width) 返回一个原字符串左对齐,并使用空格填充至长度 width 的新字符串。
13、string.rjust(width) 返回一个原字符串右对齐,并使用空格填充至长度 width 的新字符串。
[python] view plain copy >>> myString = "Hello world" >>> myString.ljust(30) 'Hello world ' >>> myString.rjust(30) ' Hello world'14、string.lower() 转换 string 中所有大写字符为小写。
15、string.upper() 转换 string 中的小写字母为大写。
16、string.swapcase() 翻转 string 中的大小写。
[python] view plain copy >>> myString = "Hello world!" >>> myString.lower() 'hello world!' >>> myString.upper() 'HELLO WORLD!' >>> myString.swapcase() 'hELLO WORLD!'17、string.lstrip() 截掉 string 字符串左边的空格。
18、string.rstrip() 删除 string 字符串末尾的空格。
19、string.strip([obj]) 在 string 上执行 lstrip()和 rstrip()
20、string.replace(str1, str2, num=string.count(str1))
把 string 中的 str1 替换成 str2,如果 num 指定,则替换不超过 num 次。
[python] view plain copy >>> myString = "This is an apple and that is an apple too." >>> myString.replace('apple', 'orange', string.count('apple')) 'This is an orange and that is an orange too.' >>> myString.replace('apple', 'orange', 1) 'This is an orange and that is an apple too.' #虽然语法有问题21、string.split(str="", num=string.count(str))
以 str 为分隔符切片 string,如果 num有指定值,则仅分隔 num 个子字符串。
[python] view plain copy >>> myString.split(' ', 3) ['This', 'is', 'an', 'apple and that is an apple too.']22、string.startswith(obj, beg=0,end=len(string)) 检查字符串是否是以 obj 开头,是则返回 True,否则返回False。如果beg 和 end 指定值,则在指定范围内检查。
[python] view plain copy >>> myString.startswith('This') True >>> myString.startswith('and') False23、string.title() 返回"标题化"的 string,就是说所有单词都是以大写开始,其余字母均为小写(见 istitle())。
24、string.istitle() 如果 string 是标题化的(见 title())则返回 True,否则返回 False。
[python] view plain copy >>> myString = "This is an apple and that is an apple too." >>> myString.istitle() False >>> str = myString.title() >>> str 'This Is An Apple And That Is An Apple Too.' >>> str.istitle() True25、string.zfill(width)
返回长度为 width 的字符串,原字符串 string 右对齐,前面填充0。
[python] view plain copy >>> myString = "Hello world!" >>> myString.zfill(30) '000000000000000000Hello world!'