Python内置的字符串处理函数整理


Posted in Python onJanuary 29, 2013

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 相关文章推荐
python实现在pickling的时候压缩的方法
Sep 25 Python
python集合类型用法分析
Apr 08 Python
Python将阿拉伯数字转换为罗马数字的方法
Jul 10 Python
Python模拟登陆淘宝并统计淘宝消费情况的代码实例分享
Jul 04 Python
Python冒泡排序注意要点实例详解
Sep 09 Python
Python操作Redis之设置key的过期时间实例代码
Jan 25 Python
python链接oracle数据库以及数据库的增删改查实例
Jan 30 Python
python中numpy的矩阵、多维数组的用法
Feb 05 Python
基于python指定包的安装路径方法
Oct 27 Python
python定位xpath 节点位置的方法
Aug 27 Python
python rsa-oaep加密的示例代码
Sep 23 Python
python logging模块的使用详解
Oct 23 Python
python每次处理固定个数的字符的方法总结
Jan 29 #Python
python设置windows桌面壁纸的实现代码
Jan 28 #Python
python连接sql server乱码的解决方法
Jan 28 #Python
python定时检查启动某个exe程序适合检测exe是否挂了
Jan 21 #Python
Python实现的金山快盘的签到程序
Jan 17 #Python
多线程爬虫批量下载pcgame图片url 保存为xml的实现代码
Jan 17 #Python
Python高效编程技巧
Jan 07 #Python
You might like
function.inc.php超越php
2006/12/09 PHP
php 过滤危险html代码
2009/06/29 PHP
Trying to clone an uncloneable object of class Imagic的解决方法
2012/01/11 PHP
CI框架学习笔记(二) -入口文件index.php
2014/10/27 PHP
Laravel使用memcached缓存对文章增删改查进行优化的方法
2016/10/08 PHP
浅谈htmlentities 、htmlspecialchars、addslashes的使用方法
2016/12/09 PHP
Yii框架使用魔术方法实现跨文件调用功能示例
2017/05/20 PHP
js/html光标定位的实现代码
2013/09/23 Javascript
解决window.opener=null;window.close(),只支持IE6不支持IE7,IE8的问题
2014/01/14 Javascript
理解javascript定时器中的单线程
2016/02/23 Javascript
Node.js的Koa框架上手及MySQL操作指南
2016/06/13 Javascript
Ajax跨域实现代码(后台jsp)
2017/01/21 Javascript
前端js中的事件循环eventloop机制详解
2019/05/15 Javascript
vue实现数据控制视图的原理解析
2020/01/07 Javascript
jquery实现直播视频弹幕效果
2020/02/25 jQuery
vue 解决setTimeOut和setInterval函数无效报错的问题
2020/07/30 Javascript
[40:13]Ti4 冒泡赛第二天 iG vs NEWBEE 2
2014/07/15 DOTA
Python图片转换成矩阵,矩阵数据转换成图片的实例
2018/07/02 Python
Python3 log10()函数简单用法
2019/02/19 Python
Python判断对象是否相等及eq函数的讲解
2019/02/25 Python
python 获取sqlite3数据库的表名和表字段名的实例
2019/07/17 Python
python递归函数求n的阶乘,优缺点及递归次数设置方式
2020/04/02 Python
python TCP包注入方式
2020/05/05 Python
python 常用日期处理-- datetime 模块的使用
2020/09/02 Python
Python使用windows设置定时执行脚本
2020/11/12 Python
python空元组在all中返回结果详解
2020/12/15 Python
用python计算文件的MD5值
2020/12/23 Python
西班牙汉普顿小姐:购买帆布鞋和太阳镜
2016/10/23 全球购物
法国设计制造的扫帚和刷子:Andrée Jardin
2018/12/06 全球购物
英语教学随笔感言
2014/02/20 职场文书
人力资源职位说明书
2014/07/29 职场文书
鲁迅故居导游词
2015/02/05 职场文书
2016元旦主持人经典开场白台词
2015/12/03 职场文书
幼儿园大班教学反思
2016/03/02 职场文书
Python jiaba库的使用详解
2021/11/23 Python
分享一个vue实现的记事本功能案例
2022/04/11 Vue.js