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 相关文章推荐
python3之微信文章爬虫实例讲解
Jul 12 Python
python实现机械分词之逆向最大匹配算法代码示例
Dec 13 Python
Python实现学生成绩管理系统
Apr 05 Python
详解如何将python3.6软件的py文件打包成exe程序
Oct 09 Python
django admin后台添加导出excel功能示例代码
May 15 Python
python绘制地震散点图
Jun 18 Python
python3实现猜数字游戏
Dec 07 Python
Python 微信爬虫完整实例【单线程与多线程】
Jul 06 Python
对Django 转发和重定向的实例详解
Aug 06 Python
python实现指定ip端口扫描方式
Dec 17 Python
使用Python实现音频双通道分离
Dec 25 Python
python实现银行账户系统
Feb 22 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/03/17 PHP
php中使用gd库实现下载网页中所有图片
2015/05/12 PHP
详解php魔术方法(Magic methods)的使用方法
2016/02/14 PHP
2020最新版 PhpStudy V8.1版本下载安装使用详解
2020/10/30 PHP
在多个页面使用同一个HTML片段的代码
2011/03/04 Javascript
autoPlay 基于jquery的图片自动播放效果
2011/12/07 Javascript
jq选项卡鼠标延迟的插件实例
2013/05/13 Javascript
z-blog SyntaxHighlighter 长代码无法换行解决办法(基于jquery)
2015/11/18 Javascript
详解Wondows下Node.js使用MongoDB的环境配置
2016/03/01 Javascript
浅谈Javascript数组(推荐)
2016/05/17 Javascript
解析微信JS-SDK配置授权,实现分享接口
2016/12/09 Javascript
微信小程序 地图map实例详解
2017/06/07 Javascript
Vue keep-alive实践总结(推荐)
2017/08/31 Javascript
详解从Vue.js源码看异步更新DOM策略及nextTick
2017/10/11 Javascript
详解vue组件基础
2018/05/04 Javascript
nodejs使用node-xlsx生成excel的方法示例
2019/08/22 NodeJs
微信小程序 button样式设置为图片的方法
2020/06/19 Javascript
js实现鼠标滑动到某个div禁止滚动
2020/09/17 Javascript
python中numpy的矩阵、多维数组的用法
2018/02/05 Python
python实现zabbix发送短信脚本
2018/09/17 Python
Python3爬虫学习之应对网站反爬虫机制的方法分析
2018/12/12 Python
基于Django框架的权限组件rbac实例讲解
2019/08/31 Python
python 中的[:-1]和[::-1]的具体使用
2020/02/13 Python
python gui开发——制作抖音无水印视频下载工具(附源码)
2021/02/07 Python
美国浴缸、水槽和水龙头购物网站:Vintage Tub & Bath
2019/11/05 全球购物
个人查摆剖析材料
2014/02/04 职场文书
食品安全汇报材料
2014/08/18 职场文书
银行竞聘上岗演讲稿
2014/09/12 职场文书
2014年药房工作总结
2014/11/22 职场文书
公司环境卫生管理制度
2015/08/05 职场文书
《包身工》教学反思
2016/02/23 职场文书
一文带你理解vue创建一个后台管理系统流程(Vue+Element)
2021/05/18 Vue.js
详解Python如何批量采集京东商品数据流程
2022/01/22 Python
「月刊Comic Alive」2022年5月号封面公开
2022/03/21 日漫
ubuntu20.04虚拟机无法上网的问题及解决
2022/12/24 Servers