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通过解析网页实现看报程序的方法
Aug 04 Python
python使用正则表达式替换匹配成功的组
Nov 17 Python
浅谈Python2获取中文文件名的编码问题
Jan 09 Python
python中的数据结构比较
May 13 Python
python操作excel让工作自动化
Aug 09 Python
执行Django数据迁移时报 1091错误及解决方法
Oct 14 Python
使用python执行shell脚本 并动态传参 及subprocess的使用详解
Mar 06 Python
keras在构建LSTM模型时对变长序列的处理操作
Jun 29 Python
python如何将图片转换素描画
Sep 08 Python
python实现canny边缘检测
Sep 14 Python
PyTorch预训练Bert模型的示例
Nov 17 Python
pycharm 复制代码出现空格的解决方式
Jan 15 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
利用phpexcel把excel导入数据库和数据库导出excel实现
2014/01/09 PHP
php表单提交实例讲解
2015/11/12 PHP
Ubuntu 16.04下安装PHP 7过程详解
2017/03/28 PHP
thinkphp5 migrate数据库迁移工具
2018/02/20 PHP
Laravel学习笔记之Artisan命令生成自定义模板的方法
2018/11/22 PHP
javascript显示选择目录对话框的代码
2008/11/10 Javascript
javascript call和apply方法
2008/11/24 Javascript
juqery 学习之四 筛选过滤
2010/11/30 Javascript
Javascript事件热键兼容ie|firefox
2010/12/30 Javascript
jquery和ajax的关系详细介绍
2013/11/29 Javascript
jQuery封装的tab选项卡插件分享
2015/06/16 Javascript
浅谈bootstrap源码分析之scrollspy(滚动侦听)
2016/06/06 Javascript
vue脚手架中配置Sass的方法
2018/01/04 Javascript
通过实例讲解JS如何防抖动
2019/06/15 Javascript
Flexible.js可伸缩布局实现方法详解
2020/11/13 Javascript
[40:55]Liquid vs LGD 2018国际邀请赛小组赛BO2 第二场 8.16
2018/08/17 DOTA
一个检测OpenSSL心脏出血漏洞的Python脚本分享
2014/04/10 Python
Python中实现常量(Const)功能
2015/01/28 Python
python利用正则表达式提取字符串
2016/12/08 Python
Python之lambda匿名函数及map和filter的用法
2019/03/05 Python
python交易记录整合交易类详解
2019/07/03 Python
CSS3 box-shadow属性实例详解
2020/06/19 HTML / CSS
HTML5 Blob对象的具体使用
2020/05/22 HTML / CSS
MADE法国:提供原创设计师家具
2018/09/18 全球购物
KARATOV珠宝在线商店:俄罗斯珠宝品牌
2019/03/13 全球购物
澳大利亚美容产品及化妆品在线:Activeskin
2020/06/03 全球购物
专科毕业生学习生活的自我评价
2013/10/26 职场文书
花店创业计划书范文
2014/02/07 职场文书
渔夫的故事教学反思
2014/02/14 职场文书
中层干部竞聘演讲稿
2014/05/15 职场文书
2014镇副书记群众路线专题民主生活会思想汇报
2014/09/23 职场文书
求职信内容一般写什么?
2015/03/20 职场文书
医药公司开票员岗位职责
2015/04/15 职场文书
刑事上诉状(量刑过重)
2015/05/23 职场文书
Python中Permission denied的解决方案
2021/04/02 Python
浅谈MySQL next-key lock 加锁范围
2021/06/07 MySQL