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的Django框架代码的一些示例
Apr 20 Python
几种实用的pythonic语法实例代码
Feb 24 Python
Python安装图文教程 Pycharm安装教程
Mar 27 Python
python 按照固定长度分割字符串的方法小结
Apr 30 Python
tensorflow: 查看 tensor详细数值方法
Jun 13 Python
深入浅析Python中list的复制及深拷贝与浅拷贝
Sep 03 Python
Python利用字典破解WIFI密码的方法
Feb 27 Python
python程序输出无内容的解决方式
Apr 09 Python
推荐技术人员一款Python开源库(造数据神器)
Jul 08 Python
在Django中使用MQTT的方法
May 10 Python
Python将CSV文件转化为HTML文件的操作方法
Jun 30 Python
python scrapy简单模拟登录的代码分析
Jul 21 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中file_get_contents函数抓取https地址出错的解决方法(两种方法)
2015/09/22 PHP
微信支付开发交易通知实例
2016/07/12 PHP
阿里云PHP SMS短信服务验证码发送方法
2017/07/11 PHP
人人网javascript面试题 可以提前实现下
2012/01/05 Javascript
jquery日历插件datepicker用法分析
2016/01/22 Javascript
jQuery组件easyui基本布局实现代码
2016/08/25 Javascript
footer定位页面底部(代码分享)
2017/03/07 Javascript
利用vueJs实现图片轮播实例代码
2017/06/03 Javascript
vue.js学习之vue-cli定制脚手架详解
2017/07/02 Javascript
vue实现树形菜单效果
2018/03/19 Javascript
vue-自定义组件传值的实例讲解
2018/09/18 Javascript
谈谈为什么你的 JavaScript 代码如此冗长
2019/01/30 Javascript
详解使用React制作一个模态框
2019/03/14 Javascript
js简单遍历获取对象中的属性值的方法示例
2019/06/19 Javascript
微信小程序 导入图标实现过程详解
2019/10/11 Javascript
js实现提交前对列表数据的增删改查
2020/01/16 Javascript
[02:42]决战东方!DOTA2亚洲邀请赛重启荣耀之争
2017/03/17 DOTA
[55:18]Liquid vs Chaos 2019国际邀请赛小组赛 BO2 第一场 8.15
2019/08/16 DOTA
[00:29]2019完美世界全国高校联赛(秋季赛)总决赛海口落幕
2019/12/10 DOTA
Python实现文件内容批量追加的方法示例
2017/08/29 Python
python中copy()与deepcopy()的区别小结
2018/08/03 Python
使用 Visual Studio Code(VSCode)搭建简单的Python+Django开发环境的方法步骤
2018/12/17 Python
Django REST framework 如何实现内置访问频率控制
2019/07/23 Python
详解python中index()、find()方法
2019/08/29 Python
python json 递归打印所有json子节点信息的例子
2020/02/27 Python
python 如何设置守护进程
2020/10/29 Python
家得宝加拿大家装网上商店:The Home Depot加拿大
2016/08/27 全球购物
英国玛莎百货澳大利亚:Marks & Spencer Australia
2019/08/30 全球购物
写一个在SQL Server创建表的SQL语句
2012/03/10 面试题
科技节口号
2014/06/19 职场文书
信息合作协议书
2014/10/09 职场文书
2014教师评职称工作总结
2014/11/10 职场文书
春晚观后感
2015/06/11 职场文书
劳保用品管理制度范本
2015/08/06 职场文书
Python Parser的用法
2021/05/12 Python
使用CSS实现黑白格背景效果
2022/06/01 HTML / CSS