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实现图书超期提醒
Aug 02 Python
python正则实现计算器功能
Dec 14 Python
机器学习经典算法-logistic回归代码详解
Dec 22 Python
详解Python 爬取13个旅游城市,告诉你五一大家最爱去哪玩?
May 07 Python
python openCV获取人脸部分并存储功能
Aug 28 Python
Python字符串和正则表达式中的反斜杠('\')问题详解
Sep 03 Python
pytorch 实现tensor与numpy数组转换
Dec 27 Python
Python re正则表达式元字符分组()用法分享
Feb 10 Python
pytorch中的weight-initilzation用法
Jun 24 Python
解决Pytorch自定义层出现多Variable共享内存错误问题
Jun 28 Python
python怎么判断素数
Jul 01 Python
详解Selenium-webdriver绕开反爬虫机制的4种方法
Oct 28 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
咖啡历史、消费和行业趋势
2021/03/03 咖啡文化
php使用反射插入对象示例分享
2014/03/11 PHP
php解析json数据实例
2014/08/19 PHP
再推荐十款免费的php开发工具
2015/11/09 PHP
详解PHP的Yii框架中扩展的安装与使用
2016/04/01 PHP
利用PHP命令行模式采集股票趋势信息
2016/08/09 PHP
基于PHP的登录和注册的功能的实现
2020/08/06 PHP
javascript String 的扩展方法集合
2008/06/01 Javascript
在jQuery中使用$而避免跟其它库产生冲突的方法
2015/08/13 Javascript
利用jQuery及AJAX技术定时更新GridView的某一列数据
2015/12/04 Javascript
Javascript基于jQuery UI实现选中区域拖拽效果
2016/11/25 Javascript
jquery 获取索引值在一定范围的列表方法
2018/01/25 jQuery
Node.js 使用jade模板引擎的示例
2018/05/11 Javascript
JavaScript面向对象的程序设计(犯迷糊的小羊)
2018/05/27 Javascript
JS实现的贪吃蛇游戏完整实例
2019/01/18 Javascript
基于node.js实现爬虫的讲解
2019/02/18 Javascript
浅谈JS的原型和继承
2019/05/08 Javascript
微信小程序云开发之使用云存储
2019/05/17 Javascript
vue数据更新UI不刷新显示的解决办法
2020/08/06 Javascript
Python实现对比不同字体中的同一字符的显示效果
2015/04/23 Python
Windows下使Python2.x版本的解释器与3.x共存的方法
2015/10/25 Python
动态规划之矩阵连乘问题Python实现方法
2017/11/27 Python
Python使用for生成列表实现过程解析
2020/09/22 Python
详解Css3新特性应用之过渡与动画
2017/01/10 HTML / CSS
html5 迷宫游戏(碰撞检测)实例一
2013/07/25 HTML / CSS
材料物理专业个人求职信
2013/12/15 职场文书
服装创业计划书范文
2014/02/05 职场文书
开展批评与自我批评发言材料
2014/10/17 职场文书
2014年个人工作总结模板
2014/12/15 职场文书
公司备用金管理制度
2015/08/04 职场文书
2016中学教师读书心得体会
2016/01/13 职场文书
30岁前绝不能错过的10本书
2019/08/08 职场文书
导游词之清晏园
2019/11/22 职场文书
MySQL系列之十 MySQL事务隔离实现并发控制
2021/07/02 MySQL
零基础学java之带参数以及返回值的方法
2022/04/10 Java/Android
Oracle中DBLink的详细介绍
2022/04/29 Oracle