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中lambda的用法及其与def的区别解析
Jul 28 Python
在Python中调用ggplot的三种方法
Apr 08 Python
解决Python传递中文参数的问题
Aug 04 Python
python 的列表遍历删除实现代码
Apr 12 Python
Python中序列的修改、散列与切片详解
Aug 27 Python
使用Django和Python创建Json response的方法
Mar 26 Python
Python使用numpy产生正态分布随机数的向量或矩阵操作示例
Aug 22 Python
python中的RSA加密与解密实例解析
Nov 18 Python
基于python实现破解滑动验证码过程解析
May 28 Python
Python调用C/C++的方法解析
Aug 05 Python
python uuid生成唯一id或str的最简单案例
Jan 13 Python
Python打包exe时各种异常处理方案总结
May 18 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
论建造顺序的重要性
2020/03/04 星际争霸
php筛选不存在的图片资源
2015/04/28 PHP
php反射学习之不用new方法实例化类操作示例
2019/06/14 PHP
关于Aptana Studio生成自动备份文件的解决办法
2009/12/23 Javascript
js清理Word格式示例代码
2014/02/13 Javascript
js随机生成网页背景颜色的方法
2015/02/26 Javascript
jquery表单验证插件(jquery.validate.js)的3种使用方式
2015/03/28 Javascript
javascript弹出窗口实现代码
2015/11/12 Javascript
基于JavaScript实现简单的随机抽奖小程序
2016/01/05 Javascript
Node.js 应用跑得更快 10 个技巧
2016/04/03 Javascript
Web安全测试之XSS实例讲解
2016/08/15 Javascript
jQuery实现鼠标选中文字后弹出提示窗口效果【附demo源码】
2016/09/05 Javascript
Radio 单选JS动态添加的选项onchange事件无效的解决方法
2016/12/12 Javascript
javascript中call,apply,bind函数用法示例
2016/12/19 Javascript
使用jsonp实现跨域获取数据实例讲解
2016/12/25 Javascript
jQuery插件HighCharts实现的2D回归直线散点效果示例【附demo源码下载】
2017/03/09 Javascript
详解vue过滤器在v2.0版本用法
2017/06/01 Javascript
JavaScript深拷贝和浅拷贝概念与用法实例分析
2018/06/07 Javascript
JavaScript强制类型转换和隐式类型转换操作示例
2019/05/01 Javascript
vue实现图片上传功能
2020/05/28 Javascript
Python 26进制计算实现方法
2015/05/28 Python
python基础入门学习笔记(Python环境搭建)
2016/01/13 Python
Python中datetime模块参考手册
2017/01/13 Python
Python中numpy模块常见用法demo实例小结
2019/03/16 Python
python之列表推导式的用法
2019/11/29 Python
Python实现电视里的5毛特效实例代码详解
2020/05/15 Python
Python生成器传参数及返回值原理解析
2020/07/22 Python
Python基于argparse与ConfigParser库进行入参解析与ini parser
2021/02/02 Python
学生自我鉴定模板
2013/12/30 职场文书
文化宣传方案
2014/03/13 职场文书
市场营销计划书范文
2015/01/16 职场文书
正规借条模板
2015/05/26 职场文书
导游词之天下银坑景区
2019/11/21 职场文书
CSS完成视差滚动效果
2021/04/27 HTML / CSS
Python+Appium实现自动抢微信红包
2021/05/21 Python
Python 视频画质增强
2022/04/28 Python