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实现2014火车票查询代码分享
Jan 10 Python
python中List的sort方法指南
Sep 01 Python
Python+django实现文件上传
Jan 17 Python
Saltstack快速入门简单汇总
Mar 01 Python
如何用python整理附件
May 13 Python
Python实现的爬取网易动态评论操作示例
Jun 06 Python
python之django母板页面的使用
Jul 03 Python
Django中信号signals的简单使用方法
Jul 04 Python
Django模型中字段属性choice使用说明
Mar 30 Python
使用OpenCV对车道进行实时检测的实现示例代码
Jun 19 Python
[原创]赚疯了!转手立赚800+?大佬的python「抢茅台脚本」使用教程
Jan 12 Python
tensorflow2.0教程之Keras快速入门
Feb 20 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
常见的PHP五种设计模式小结
2011/03/23 PHP
PHP制作百度词典查词采集器
2015/01/29 PHP
PHPCMS手机站伪静态设置详细教程
2017/02/06 PHP
随机显示经典句子或诗歌的javascript脚本
2007/08/04 Javascript
jquery $.ajax入门应用一
2008/11/19 Javascript
百度留言本js 大家可以参考下
2009/10/13 Javascript
用jquery与css打造个性化的单选框和复选框
2010/10/20 Javascript
精通Javascript系列之数值计算
2011/06/07 Javascript
javascript 利用Image对象实现的埋点(某处的点击数)统计
2012/12/28 Javascript
排序算法的javascript实现与讲解(99js手记)
2014/09/28 Javascript
jquery mobile界面数据刷新的实现方法
2016/05/28 Javascript
AngularJS中$http服务常用的应用及参数
2016/08/22 Javascript
使用JavaScript获取Request中参数的值方法
2016/09/27 Javascript
Validform验证时可以为空否则按照指定格式验证
2017/10/20 Javascript
EasyUI的DataGrid绑定Json数据源的示例代码
2017/12/16 Javascript
JQuery Ajax执行跨域请求数据的解决方案
2018/12/10 jQuery
[01:10]DOTA2次级职业联赛 - Fly战队宣传片
2014/12/01 DOTA
在Python的Flask框架下收发电子邮件的教程
2015/04/21 Python
python交互式图形编程实例(三)
2017/11/17 Python
Python 查看文件的编码格式方法
2017/12/21 Python
Win7 64位下python3.6.5安装配置图文教程
2020/10/27 Python
python3实现163邮箱SMTP发送邮件
2018/05/22 Python
pandas 快速处理 date_time 日期格式方法
2018/11/12 Python
python实现几种归一化方法(Normalization Method)
2019/07/31 Python
tensorflow2.0与tensorflow1.0的性能区别介绍
2020/02/07 Python
python模式 工厂模式原理及实例详解
2020/02/11 Python
20行代码教你用python给证件照换底色的方法示例
2021/02/05 Python
定义一结构体变量,用其表示点坐标,并输入两点坐标,求两点之间的距离
2015/08/17 面试题
应届毕业生求职信范文分享
2013/12/26 职场文书
八年级数学教学反思
2014/01/31 职场文书
李白故里导游词
2015/02/12 职场文书
建党伟业的观后感
2015/06/01 职场文书
安娜卡列尼娜观后感
2015/06/11 职场文书
导游词之无锡华莱坞
2019/12/02 职场文书
用React Native制作一个简单的游戏引擎
2021/05/27 Javascript
Nginx本地配置SSL访问的实例教程
2022/05/30 Servers