Python String Services
来源:互联网 发布:淘宝类目珠宝上不去 编辑:程序博客网 时间:2024/05/29 16:55
欢迎关注我的个人博客blog.timene.com
处理字符串是每天必须的工作,官方文档地址http://docs.python.org/library/stdtypes.html#string-methods
str.capitalize()
字符串首字母大写
>>> str = 'wahaha wahaha'
>>> str.capitalize()
'Wahaha wahaha'
str.center(width[, fillchar])
返回一个width长,str居中的字符串,扩充的字符用fillchar填充
>>> str = 'wahaha wahaha'
>>> str.center(20)
' wahaha wahaha '
>>> str.center(20, '-')
'---wahaha wahaha----'
>>> str.center(5)
'wahaha wahaha'
str.count(sub[, start[, end]])
统计在str中查找sub出现的次数
>>> str = 'wahaha wahaha'
>>> str.count('ha')
4
>>> str.count('wa')
2
>>> str.count('wh')
0
>>> str.count('ha', 0, 4)
1
>>> str.count('ha', 0, 6)
2
str.startswith(prefix[, start[, end]]), str.endswith(suffix[, start[, end]])
判断字符串是否以prefix开始,判断字符串是否以suffix结尾
>>> str = 'wahaha wahaha'
>>> str.endswith('a')
True
>>> str.endswith('ha')
True
>>> str.endswith('wa')
False
>>> str.endswith(('ha', 'wa'))
True
str.expandtabs([tabsize])
转换tab为空格
>>> str = 'wahaha wahaha'
>>> str.expandtabs()
'wahaha wahaha'
>>> str.expandtabs(8)
'wahaha wahaha'
>>> str.expandtabs(4)
'wahaha wahaha'
str.find(sub[, start[, end]]) ,str.rfind(sub[, start[, end]])
判断str中sub首次出现的位置。注意,如果要判断sub是否出现在str中,使用in,比如 'Py'in'Python' 返回True
>>> str = 'wahaha wahaha'
>>> str.find('ha')
2
>>> str.find('wh')
-1
>>> str.find('ha', 5)
9
str.format(*args, **kwargs)
格式化字符串,个人觉得有点像是字符串模版的替换。其他高级功能有待学习
>>> str = "The sum of 1 + 2 is {0}"
>>> str.format(3)
'The sum of 1 + 2 is 3'
str.index(sub[, start[, end]]) , str.rindex(sub[, start[, end]])
功能同find,不同的当str中没有sub时,find返回-1,index抛出ValueError
>>> str = 'wahaha wahaha'
>>> str.index('ha')
2
>>> str.index('wh')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found
str.join(iterable)
使用str连接iterable,用来将list,tuple,dictionary。。。拼接成字符串
>>> iter = ['wa', 'ha', 'ha', 'wa', 'ha', 'ha']
>>> ','.join(iter)
'wa,ha,ha,wa,ha,ha'
>>> ';'.join(iter)
'wa;ha;ha;wa;ha;ha'
>>> ' '.join(iter)
'wa ha ha wa ha ha'
str.ljust(width[, fillchar]),str.rjust(width[, fillchar])
填充字符串到width长度
>>> str = 'wahaha'
>>> str.ljust(10)
'wahaha '
>>> str.ljust(10, '-')
'wahaha----'
>>> str.rjust(10)
' wahaha'
>>> str.rjust(10, '-')
'----wahaha'
str.lower(),str.upper()
转换str的大写为小写,转换str中的小写为大写
>>> str = 'WaHaHa wAhAhA'
>>> str.lower()
'wahaha wahaha'
str.strip([chars]),str.lstrip([chars]),str.rstrip([chars])
过滤str两边的字符,请注意参数的使用
>>> ' spacious '.strip()
'spacious'
>>> ' spacious '.lstrip()
'spacious '
>>> ' spacious '.rstrip()
' spacious'
>>> 'www.example.com'.lstrip('cmowz.')
'example.com'
>>> 'www.example.com'.rstrip('cmowz.')
'www.example'
>>> 'www.example.com'.strip('cmowz.')
'example'
str.partition(sep),str.rpartition(sep)
在第一次出现sep的地方分割字符串,返回值是一个有三个元素的tuple
>>> str = 'wahaha wahaha'
>>> str.partition(' ')
('wahaha', ' ', 'wahaha')
>>> str = 'wahaha wahaha wahaha'
>>> str.partition(' ')
('wahaha', ' ', 'wahaha wahaha')
str.replace(old, new[, count])
字符串替换,第三个参数控制替换的次数
>>> str = 'wahaha wahaha'
>>> str.replace('ha', 'world')
'waworldworld waworldworld'
>>> str.replace('ha', 'world', 2)
'waworldworld wahaha'
str.split([sep[, maxsplit]]),str.rsplit([sep[, maxsplit]])
分割字符串
>>> str = 'wahaha wahaha wahaha'
>>> str.split()
['wahaha', 'wahaha', 'wahaha']
>>> str = 'wahaha-wahaha-wahaha'
>>> str.split('-')
['wahaha', 'wahaha', 'wahaha']
>>> str.rsplit('-')
['wahaha', 'wahaha', 'wahaha']
>>> str.split('-', 1)
['wahaha', 'wahaha-wahaha']
>>> str.rsplit('-', 1)
['wahaha-wahaha', 'wahaha']
str.splitlines([keepends])
按行分割字符串
>>> 'ab c\n\nde fg\rkl\r\n'.splitlines()
['ab c', '', 'de fg', 'kl']
>>> 'ab c\n\nde fg\rkl\r\n'.splitlines(True)
['ab c\n', '\n', 'de fg\r', 'kl\r\n']
str.swapcase()
大写字母变小写,小写字母变大写
>>> str = 'WaHaHa wAhAhA'
>>> str.swapcase()
'wAhAhA WaHaHa'
str.title()
每个单词的首字母大写,其他字母小写
>>> str = 'this is WaHaHa wAhAhA'
>>> str.title()
'This Is Wahaha Wahaha'
str.translate(table[, deletechars])
字符的转换和删除,类似原始的加密
>>> 'read this short text'.translate(None, 'aeiou')
'rd ths shrt txt'
str.zfill(width)
用0填充,使len(str) == witdh,类似rjust(width, '0')
>>> str.zfill(10)
'0000wahaha'
>>> str.zfill(5)
'wahaha'
- Python String Services
- Python - string
- Python string
- 【Python】 string
- Python---string
- python String
- python string
- python string
- Services
- services
- Services
- services
- Services
- Services
- Services
- Services
- Services
- Services
- MDL
- C++中 assert的用法
- xcode调试程序
- Linux 视频设备驱动V4L2最常用的控制命令使用说明(1.02)
- DebugPrint格式输出
- Python String Services
- HDU 2084 数塔
- 面向对象和面向过程的区别
- jquery实现智能感知式搜索
- 2440存储系统记录
- jquery插件validate内置验证规则
- 新浪博客开发 授权认证 OAuth2.0
- ObReferenceObjectByHandle内核函数
- Fatal signal 11 (SIGSEGV) at 0xdeadbaad (code=1) 错误 解决方案(android-ndk)