Python内置的字符串处理函数整理


Posted in Python onJanuary 29, 2013

str='python String function'

生成字符串变量str='python String function'

字符串长度获取:len(str)
例:print '%s length=%d' % (str,len(str))

字母处理
全部大写:str.upper()
全部小写:str.lower()
大小写互换:str.swapcase()
首字母大写,其余小写:str.capitalize()
首字母大写:str.title()
print '%s lower=%s' % (str,str.lower())
print '%s upper=%s' % (str,str.upper())
print '%s swapcase=%s' % (str,str.swapcase())
print '%s capitalize=%s' % (str,str.capitalize())
print '%s title=%s' % (str,str.title())
格式化相关
获取固定长度,右对齐,左边不够用空格补齐:str.ljust(width)
获取固定长度,左对齐,右边不够用空格补齐:str.ljust(width)
获取固定长度,中间对齐,两边不够用空格补齐:str.ljust(width)
获取固定长度,右对齐,左边不足用0补齐
print '%s ljust=%s' % (str,str.ljust(20))
print '%s rjust=%s' % (str,str.rjust(20))
print '%s center=%s' % (str,str.center(20))
print '%s zfill=%s' % (str,str.zfill(20))

字符串搜索相关
搜索指定字符串,没有返回-1:str.find('t')
指定起始位置搜索:str.find('t',start)
指定起始及结束位置搜索:str.find('t',start,end)
从右边开始查找:str.rfind('t')
搜索到多少个指定字符串:str.count('t')
上面所有方法都可用index代替,不同的是使用index查找不到会抛异常,而find返回-1
print '%s find nono=%d' % (str,str.find('nono'))
print '%s find t=%d' % (str,str.find('t'))
print '%s find t from %d=%d' % (str,1,str.find('t',1))
print '%s find t from %d to %d=%d' % (str,1,2,str.find('t',1,2))
#print '%s index nono ' % (str,str.index('nono',1,2))
print '%s rfind t=%d' % (str,str.rfind('t'))
print '%s count t=%d' % (str,str.count('t'))

字符串替换相关
替换old为new:str.replace('old','new')
替换指定次数的old为new:str.replace('old','new',maxReplaceTimes)
print '%s replace t to *=%s' % (str,str.replace('t', '*'))
print '%s replace t to *=%s' % (str,str.replace('t', '*',1))

字符串去空格及去指定字符
去两边空格:str.strip()
去左空格:str.lstrip()
去右空格:str.rstrip()
去两边字符串:str.strip('d'),相应的也有lstrip,rstrip
str=' python String function '
print '%s strip=%s' % (str,str.strip())
str='python String function'
print '%s strip=%s' % (str,str.strip('d'))

按指定字符分割字符串为数组:str.split(' ')

默认按空格分隔
str='a b c de'
print '%s strip=%s' % (str,str.split())
str='a-b-c-de'
print '%s strip=%s' % (str,str.split('-'))

字符串判断相关
是否以start开头:str.startswith('start')
是否以end结尾:str.endswith('end')
是否全为字母或数字:str.isalnum()
是否全字母:str.isalpha()
是否全数字:str.isdigit()
是否全小写:str.islower()
是否全大写:str.isupper()
str='python String function'
print '%s startwith t=%s' % (str,str.startswith('t'))
print '%s endwith d=%s' % (str,str.endswith('d'))
print '%s isalnum=%s' % (str,str.isalnum())
str='pythonStringfunction'
print '%s isalnum=%s' % (str,str.isalnum())
print '%s isalpha=%s' % (str,str.isalpha())
print '%s isupper=%s' % (str,str.isupper())
print '%s islower=%s' % (str,str.islower())
print '%s isdigit=%s' % (str,str.isdigit())
str='3423'
print '%s isdigit=%s' % (str,str.isdigit())

还有其他常见的Python字符串处理 函数的话不定期更新。

Python 相关文章推荐
python对数组进行反转的方法
May 20 Python
浅谈Python单向链表的实现
Dec 24 Python
python安装与使用redis的方法
Apr 19 Python
有趣的python小程序分享
Dec 05 Python
十分钟利用Python制作属于你自己的个性logo
May 07 Python
点球小游戏python脚本
May 22 Python
Python+selenium实现自动循环扔QQ邮箱漂流瓶
May 29 Python
python定向爬虫校园论坛帖子信息
Jul 23 Python
基于pycharm导入模块显示不存在的解决方法
Oct 13 Python
python制作填词游戏步骤详解
May 05 Python
Django使用Jinja2模板引擎的示例代码
Aug 09 Python
Django2.1.7 查询数据返回json格式的实现
Dec 29 Python
python每次处理固定个数的字符的方法总结
Jan 29 #Python
python设置windows桌面壁纸的实现代码
Jan 28 #Python
python连接sql server乱码的解决方法
Jan 28 #Python
python定时检查启动某个exe程序适合检测exe是否挂了
Jan 21 #Python
Python实现的金山快盘的签到程序
Jan 17 #Python
多线程爬虫批量下载pcgame图片url 保存为xml的实现代码
Jan 17 #Python
Python高效编程技巧
Jan 07 #Python
You might like
php 代码优化之经典示例
2011/03/24 PHP
CURL的学习和应用(附多线程实现)
2013/06/03 PHP
Thinkphp 框架基础之源码获取、环境要求与目录结构分析
2020/04/27 PHP
Javascript常考语句107条收集
2010/03/09 Javascript
javascript两种function的定义介绍及区别说明
2013/05/02 Javascript
js强制把网址设为默认首页
2015/09/29 Javascript
如何防止JavaScript自动插入分号
2015/11/05 Javascript
Javascript中Date类型和Math类型详解
2016/02/27 Javascript
JavaScript编写Chrome扩展实现与浏览器的交互及时间通知
2016/05/16 Javascript
JS实现图片剪裁并预览效果
2016/08/12 Javascript
angularjs 中$apply,$digest,$watch详解
2016/10/13 Javascript
json数据处理及数据绑定
2017/01/25 Javascript
jQuery制作全屏宽度固定高度轮播图(实例讲解)
2017/07/08 jQuery
浅谈vue,angular,react数据双向绑定原理分析
2017/11/28 Javascript
vue父组件异步获取数据传给子组件的方法
2018/07/26 Javascript
解决vuejs项目里css引用背景图片不能显示的问题
2018/09/13 Javascript
vue组件中watch props根据v-if动态判断并挂载DOM的问题
2019/05/12 Javascript
[02:54]辉夜杯主赛事第二日败者组 iG.V赛后采访
2015/12/26 DOTA
[01:00:11]DOTA2-DPC中国联赛 正赛 CDEC vs DLG BO3 第一场 2月7日
2021/03/11 DOTA
python实现基于两张图片生成圆角图标效果的方法
2015/03/26 Python
关于Python中异常(Exception)的汇总
2017/01/18 Python
Python基于TCP实现会聊天的小机器人功能示例
2018/04/09 Python
DataFrame中的object转换成float的方法
2018/04/10 Python
浅谈Django中的数据库模型类-models.py(一对一的关系)
2018/05/30 Python
在pycharm中为项目导入anacodna环境的操作方法
2020/02/12 Python
python如何绘制疫情图
2020/09/16 Python
中东地区为妈妈们提供一切的头号购物目的地:Sprii
2018/05/06 全球购物
Delphi工程师笔试题
2013/09/21 面试题
大学旷课检讨书
2014/01/28 职场文书
诚信承诺书模板
2014/05/26 职场文书
节约用水标语
2014/06/11 职场文书
园林专业毕业生自荐信
2014/07/04 职场文书
九一八事变演讲稿
2014/09/05 职场文书
医药公司开票员岗位职责
2015/04/15 职场文书
2015年三好一满意工作总结
2015/07/24 职场文书
php解析非标准json、非规范json的方式实例
2022/05/10 PHP