Python中常用的字符串内建函数
来源:互联网 发布:战地3狙击枪械数据 编辑:程序博客网 时间:2024/05/22 04:54
1、string.capitalize()
把字符串的第一个字符大写。
>>> 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中常用的字符串内建函数
- Python中常用的字符串内建函数
- Python中常用的字符串内建函数
- Python中常用的内建函数
- python中常用的内建函数
- python之字符串常用内建函数
- Python 中字符串内建函数的一些使用注意事项
- python中常用的列表类型内建函数
- python中常用的字典内建函数
- python中常用的字典内建函数
- Python字符串类型的内建函数
- Python字符串类型的内建函数
- Python 的字符串内建函数
- Python 几个常用的内建函数
- python常用的几个内建函数
- Python字符串内建函数
- 【python】 字符串内建函数
- python 字符串内建函数
- 编程精粹--编写高质量C语言代码(1):假想编译程序
- [转]cocos2d-x Socket
- 怎样在ubuntu中把windows设为默认启动项
- Java学习笔记_10_继承和多态
- 类成员函数,虚函数及虚函数表,内存布局
- Python中常用的字符串内建函数
- UVA Parentheses Balance
- 【中断异常】中断的响应和服务
- 关于Android中解析xml文件qName和localName的问题
- RPM 软件管理程序: rpm
- HTTP状态码
- 如何修改mysql root密码
- Java学习笔记_11_抽象类和接口
- JDK,JRE,JVM区别与联系