Python内置的字符串处理函数详细整理(覆盖日常所用)


Posted in Python onAugust 19, 2014

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中正则表达式的详细教程
Apr 30 Python
详解设计模式中的工厂方法模式在Python程序中的运用
Mar 02 Python
浅析Python pandas模块输出每行中间省略号问题
Jul 03 Python
python定向爬虫校园论坛帖子信息
Jul 23 Python
解决pycharm 误删掉项目文件的处理方法
Oct 22 Python
Python3使用Matplotlib 绘制精美的数学函数图形
Apr 11 Python
Python简易计算器制作方法代码详解
Oct 31 Python
基于python traceback实现异常的获取与处理
Dec 13 Python
django实现HttpResponse返回json数据为中文
Mar 27 Python
Python文本文件的合并操作方法代码实例
Mar 31 Python
浅谈keras.callbacks设置模型保存策略
Jun 18 Python
详解Python 函数参数的拆解
Sep 02 Python
Python中列表(list)操作方法汇总
Aug 18 #Python
Python中多线程thread与threading的实现方法
Aug 18 #Python
Python使用函数默认值实现函数静态变量的方法
Aug 18 #Python
Python中正则表达式的用法实例汇总
Aug 18 #Python
python中enumerate的用法实例解析
Aug 18 #Python
Python采用raw_input读取输入值的方法
Aug 18 #Python
Python中Collection的使用小技巧
Aug 18 #Python
You might like
thinkphp验证码显示不出来的解决方法
2014/03/29 PHP
浅析get与post的一些特殊情况
2014/07/28 PHP
MyEclipse常用配置图文教程
2014/09/11 PHP
PHP正则匹配操作简单示例【preg_match_all应用】
2017/07/10 PHP
phpStudy配置多站点多域名和多端口的方法
2017/09/01 PHP
Docker搭建自己的PHP开发环境
2018/02/24 PHP
基于JQuery实现相同内容合并单元格的代码
2011/01/12 Javascript
js拦截alert对话框另类应用
2013/01/16 Javascript
js和as的稳定传值问题解决
2013/07/14 Javascript
JS父页面与子页面相互传值方法
2014/03/05 Javascript
JavaScript给每一个li节点绑定点击事件的实现方法
2016/12/01 Javascript
Bootstrap弹出框(Popover)被挤压的问题小结
2017/07/11 Javascript
详解关于react-redux中的connect用法介绍及原理解析
2017/09/11 Javascript
JavaScript数据结构与算法之检索算法示例【二分查找法、计算重复次数】
2019/02/22 Javascript
koa2的中间件功能及应用示例
2020/03/05 Javascript
Element PageHeader页头的使用方法
2020/07/26 Javascript
js实现淘宝浏览商品放大镜功能
2020/10/28 Javascript
Python中的字典与成员运算符初步探究
2015/10/13 Python
Python实现修改IE注册表功能示例
2018/05/10 Python
redis数据库及与python交互用法简单示例
2019/11/01 Python
Python3爬虫中Ajax的用法
2020/07/10 Python
如何向scrapy中的spider传递参数的几种方法
2020/11/18 Python
CSS3动画:5种预载动画效果实例
2017/04/05 HTML / CSS
巴西补充剂和维生素购物网站:Natue
2019/06/17 全球购物
js实现弹框效果
2021/03/24 Javascript
销售冠军获奖感言
2014/02/03 职场文书
中秋寄语大全
2014/04/11 职场文书
中药学专业求职信
2014/05/31 职场文书
群众路线自我剖析及整改措施
2014/11/04 职场文书
2014年后备干部工作总结
2014/12/08 职场文书
春季运动会开幕词
2015/01/28 职场文书
幼师大班个人总结
2015/02/13 职场文书
在职证明范本
2015/06/15 职场文书
高中生综合素质评价范文
2015/08/18 职场文书
Python torch.flatten()函数案例详解
2021/08/30 Python
使用Nginx搭载rtmp直播服务器的方法
2021/10/16 Servers