Python中常用的字符串内建函数
2014-05-09 23:59
661 查看
1、string.capitalize()
把字符串的第一个字符大写。
2、string.center(width)
返回一个原字符串居中,并使用空格填充至长度width的新字符串。可以改变填充使用的字符。
3、string.count(str, beg=0, end=len(string))
返回 str 在 string 里面出现的次数,如果 beg 或者 end 指定则返回指定范围内 str 出现的次数。
4、string.find(str, bg=0, end=len(string))
find方法可以检测str是否包含在string中,如果bg和end制定范围,则检查是否包含在指定范围内,如果是则返回开始的索引值,否则返回-1。
5、string.rfind(str, beg=0,end=len(string))
类似于 find()函数,不过是从右边开始查找。返回str左边开始的索引值。
6、string.index(str, beg=0,end=len(string)) 和find()方法一样,但是如果str不在string中会报一个异常。
7、string.rindex(str, beg=0,end=len(string)) 和index()一样,但是是从右开始。
8、string.islower()
如果 string 中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是小写,则返回 True,否则返回 False。
9、string.isupper()
如果 string 中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是大写,则返回 True,否则返回 False。
10、string.isnumeric()
如果 string 中只包含数字字符,则返回 True,否则返回 False。
11、string.join(seq)
Merges (concatenates) 以string 作为分隔符,将 seq 中所有的元素(的字符串表示)合并为一个新的字符串。
12、string.ljust(width)
返回一个原字符串左对齐,并使用空格填充至长度 width 的新字符串。
13、string.rjust(width)
返回一个原字符串右对齐,并使用空格填充至长度 width 的新字符串。
14、string.lower() 转换 string 中所有大写字符为小写。
15、string.upper() 转换 string 中的小写字母为大写。
16、string.swapcase() 翻转 string 中的大小写。
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 次。
21、string.split(str="", num=string.count(str))
以 str 为分隔符切片 string,如果 num有指定值,则仅分隔 num 个子字符串。
22、string.startswith(obj, beg=0,end=len(string))
检查字符串是否是以 obj 开头,是则返回 True,否则返回False。如果beg 和 end 指定值,则在指定范围内检查。
23、string.title()
返回"标题化"的 string,就是说所有单词都是以大写开始,其余字母均为小写(见 istitle())。
24、string.istitle()
如果 string 是标题化的(见 title())则返回 True,否则返回 False。
25、string.zfill(width)
返回长度为 width 的字符串,原字符串 string 右对齐,前面填充0。
把字符串的第一个字符大写。
>>> myString = "hello world, hello everyone!" >>> myString.capitalize() 'Hello world, hello everyone!'
2、string.center(width)
返回一个原字符串居中,并使用空格填充至长度width的新字符串。可以改变填充使用的字符。
>>> 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 出现的次数。
>>> myString.count('hello') 2 >>> myString.count('hello', 0, 10) 1
4、string.find(str, bg=0, end=len(string))
find方法可以检测str是否包含在string中,如果bg和end制定范围,则检查是否包含在指定范围内,如果是则返回开始的索引值,否则返回-1。
>>> myString.find('world') 6 >>> myString.find('am') -1
5、string.rfind(str, beg=0,end=len(string))
类似于 find()函数,不过是从右边开始查找。返回str左边开始的索引值。
>>> 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()一样,但是是从右开始。
>>> 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 found
8、string.islower()
如果 string 中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是小写,则返回 True,否则返回 False。
9、string.isupper()
如果 string 中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是大写,则返回 True,否则返回 False。
>>> myString.islower() True >>> myString.isupper() False
10、string.isnumeric()
如果 string 中只包含数字字符,则返回 True,否则返回 False。
11、string.join(seq)
Merges (concatenates) 以string 作为分隔符,将 seq 中所有的元素(的字符串表示)合并为一个新的字符串。
>>> myString = "+" >>> seq = ['8', '9'] >>> myString.join(seq) '8+9'
12、string.ljust(width)
返回一个原字符串左对齐,并使用空格填充至长度 width 的新字符串。
13、string.rjust(width)
返回一个原字符串右对齐,并使用空格填充至长度 width 的新字符串。
>>> 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 中的大小写。
>>> 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 次。
>>> 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 个子字符串。
>>> 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 指定值,则在指定范围内检查。
>>> myString.startswith('This') True >>> myString.startswith('and') False
23、string.title()
返回"标题化"的 string,就是说所有单词都是以大写开始,其余字母均为小写(见 istitle())。
24、string.istitle()
如果 string 是标题化的(见 title())则返回 True,否则返回 False。
>>> 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() True
25、string.zfill(width)
返回长度为 width 的字符串,原字符串 string 右对齐,前面填充0。
>>> myString = "Hello world!" >>> myString.zfill(30) '000000000000000000Hello world!'
相关文章推荐
- Python语法第4讲:字符串及常用函数
- Python3 字符串常用的函数
- PYTHON字符串常用函数
- Python修炼--常用字符串、数学函数 分类: python基础学习 2013-06-16 13:23 205人阅读 评论(0) 收藏
- Python常用字符串替换函数strip、replace及sub用法示例
- Python修炼--常用字符串、数学函数
- python 字符串常用函数方法整合
- Python中常用操作字符串的函数与方法总结
- 第19课 Python字符串常用函数
- 收集常用的Python 内置的各种字符串处理 函数的使用方法
- python 字符串格式化以及常用函数总结
- python基础--字符串常用函数
- Python字符串常用函数
- Python学习笔记 —— 字符串及常用函数
- Python中常用的字符串内建函数
- python学习总结---列表、元组、字符串、字典中一些常用函数
- python中常用检测字符串相关函数汇总
- Python中常用字符串 函数-转
- Python修炼--常用字符串、数学函数
- Python str字符串常用到的函数