python字符串常用方法


Posted in Python onJune 14, 2018

1、 isalnum() :判断字符串所有的字符都是字母或者数字。返回true和false

In [1]: str1='jiangwei520'
In [2]: str2='jiang wei'
In [3]: str3='520'
In [4]: str4='520 1314'
In [5]: str1.isalnum()
Out[5]: True
In [6]: str2.isalnum()
Out[6]: False
In [7]: str3.isalnum()
Out[7]: True
In [8]: str4.isalnum()
Out[8]: False

2、 isalpha() :判断字符串所有的字符都是字母。返回true和false

In [11]: s1='j w'
In [12]: s2='jw'
In [13]: s1.isalpha()
Out[13]: False
In [14]: s2.isalpha()
Out[14]: True

3、 isdigit(): 判断字符串所有的字符都是数字。返回true和false

In [15]: n1='12 34'
In [16]: n2='1234'
In [17]: n3='1.1'
In [18]: n1.isdigit()
Out[18]: False
In [19]: n2.isdigit()
Out[19]: True
In [20]: n3.isdigit()
Out[20]: False

4、 islower() :判断所有的字符都是小写。

In [23]: s1='j w'
In [24]: s2='jw'
In [25]: s3='JW'
In [26]: s1.islower()
Out[26]: True
In [27]: s2.islower()
Out[27]: True
In [28]: s3.islower()
Out[28]: False

5、 isupper() :判断所有的字符都是大写。

In [29]: s1='J w'
In [30]: s2="J W"
In [31]: s3="JW"
In [32]: s4='Jw'
In [33]: s1.isupper()
Out[33]: False
In [34]: s2.isupper()
Out[34]: True
In [35]: s3.isupper()
Out[35]: True
In [36]: s4.isupper()
Out[36]: False

6、 istitle() :判断每个单词的首字母都是大写。

In [37]: s1='hello world'
In [38]: s2='Hello World'
In [39]: s3='Hello,world'
In [40]: s4='HELLO WORLD'
In [41]: s1.istitle()
Out[41]: False
In [42]: s2.istitle()
Out[42]: True
In [43]: s3.istitle()
Out[43]: False
In [44]: s4.istitle()
Out[44]: False

7、 lower() :转小写

In [47]: s4
Out[47]: 'HELLO WORLD'
In [48]: s4.lower()
Out[48]: 'hello world'
In [49]: s2
Out[49]: 'Hello World'
In [50]: s2.lower()
Out[50]: 'hello world'

7、 upper() :转大写

In [54]: s1
Out[54]: 'HEllo WOrld'
In [55]: s3
Out[55]: 'Hello,world'
In [56]: s1.upper()
Out[56]: 'HELLO WORLD'
In [57]: s3.upper()
Out[57]: 'HELLO,WORLD'

8、 strip([chars]) :去除

lstrip()和 rstrip() 类似
In [61]: s1='  hello     world   !!!  '
In [62]: s1.strip()
Out[62]: 'hello     world   !!!'
In [63]: s2='**** jw---love---you ****'
In [64]: s2.strip('*')
Out[64]: ' jw---love---you '
#应该是去除两边的
 In [107]: a='***111***'
 In [108]: a.lstrip('*')
 Out[108]: '111***'
 In [109]: a.rstrip('*')
 Out[109]: '***111'
 In [110]: a.strip('*')
 Out[110]: '111'

9、 replace(old ,new, [count]) :替换

In [72]: a='小喵和小九'
In [73]: a.replace('喵','喵喵').replace('九','九九')
Out[73]: '小喵喵和小九九'
In [74]: b='jiangwei is a good good good boy'
In [75]: b.replace('good','nice')
Out[75]: 'jiangwei is a nice nice nice boy'
In [76]: b.replace('good','nice',2)
Out[76]: 'jiangwei is a nice nice good boy'
In [77]: b.replace('good','nice',1)
Out[77]: 'jiangwei is a nice good good boy'

10、 split() :切割。返回列表。

In [92]: path1
Out[92]: 'a/b/c/d'
In [93]: path1.split('/')
Out[93]: ['a', 'b', 'c', 'd']
In [88]: path='/home/centos/python3.6'
In [89]: path
Out[89]: '/home/centos/python3.6'
In [90]: path.split('/')
Out[90]: ['', 'home', 'centos', 'python3.6']

11、 startswith() :以指定的字符串开头。发货true和false。

endswith():类似
In [94]: a='helloworld'
In [95]: b='hello world'
In [96]: a.startswith('hello')
Out[96]: True
In [97]: b.startswith('hello')
Out[97]: True
In [98]: b.startswith('he')
Out[98]: True

12、 format() :格式化输出

In [111]: print('{name}---->{age}'.format(name='xjm',age=21))
xjm---->21
In [112]:

13、 title() : 把每个字符串的首字母大写

In [112]: s='hello world python '
In [113]: s.title()
Out[113]: 'Hello World Python '
#与capitalize不同。就第一个单词的首字母大写
 In [128]: a='hello world python'
In [129]: a.capitalize()
Out[129]: 'Hello world python'

14、 join() :插入

In [117]: a='.'
In [118]: a.join('jwlove')
Out[118]: 'j.w.l.o.v.e'
 In [124]: a='/'
In [125]: b=('user','local') 
In [127]: a.join(b)
Out[127]: 'user/local'

15、 center(width,char) :扩充

In [137]: a='Linux'
In [138]: a.center(25,'*')
Out[138]: '**********Linux**********'
In [140]: a.center(25)
Out[140]: '     Linux    '
#ljust和rjust类似
 In [142]: a.ljust(10,'-')
 Out[142]: 'Linux-----'
 In [143]: a.rjust(10,'-')
 Out[143]: '-----Linux'

16、 splitlines(): 根据\r  \n  \r\n  切割。返回列表

In [151]: a='如果\n没有\r如果'
In [154]: print(a)
如果
如果
In [157]: a.splitlines()
Out[157]: ['如果', '没有', '如果']

17、 format_map() :格式化输出

In [158]: a='hello world {course}'
In [160]: course1='python'
In [161]: course2='java'
In [178]: a.format(course=course1)
Out[178]: 'hello world java'
In [179]: a.format(course=course2)
Out[179]: 'hello world python'
In [181]: a.format_map(vars())
Out[181]: 'hello world python'

总结

以上所述是小编给大家介绍的python字符串常用方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!

Python 相关文章推荐
python通过自定义isnumber函数判断字符串是否为数字的方法
Apr 23 Python
在Python的Django框架中显示对象子集的方法
Jul 21 Python
Python基于matplotlib绘制栈式直方图的方法示例
Aug 09 Python
python实现人脸识别代码
Nov 08 Python
Python/ArcPy遍历指定目录中的MDB文件方法
Oct 27 Python
python协程之动态添加任务的方法
Feb 19 Python
python实现连连看辅助之图像识别延伸
Jul 17 Python
Python实现图片批量加入水印代码实例
Nov 30 Python
python使用pandas抽样训练数据中某个类别实例
Feb 28 Python
python 截取XML中bndbox的坐标中的图像,另存为jpg的实例
Mar 10 Python
三步解决python PermissionError: [WinError 5]拒绝访问的情况
Apr 22 Python
总结三种用 Python 作为小程序后端的方式
May 02 Python
tensorflow 输出权重到csv或txt的实例
Jun 14 #Python
修复 Django migration 时遇到的问题解决
Jun 14 #Python
tensorflow 获取模型所有参数总和数量的方法
Jun 14 #Python
tensorflow 获取变量&打印权值的实例讲解
Jun 14 #Python
利用python对Excel中的特定数据提取并写入新表的方法
Jun 14 #Python
Python基于最小二乘法实现曲线拟合示例
Jun 14 #Python
详解python之协程gevent模块
Jun 14 #Python
You might like
php打造属于自己的MVC框架
2012/03/07 PHP
php中检查文件或目录是否存在的代码小结
2012/10/22 PHP
Thinkphp批量更新数据的方法汇总
2016/06/29 PHP
php获取给定日期相差天数的方法分析
2017/02/20 PHP
laravel5.1框架model类查询的实现方法
2019/10/08 PHP
脚本吧 - 幻宇工作室用到js,超强推荐share.js
2006/12/23 Javascript
js onclick事件传参讲解
2013/11/06 Javascript
js取模(求余数)隔行变色
2014/05/15 Javascript
面向切面编程(AOP)的理解
2015/05/01 Javascript
javascript下拉列表中显示树形菜单的实现方法
2015/11/17 Javascript
JavaScript笔记之数据属性和存储器属性
2016/03/31 Javascript
早该知道的7个JavaScript技巧
2016/06/21 Javascript
微信小程序  Mustache语法详细介绍
2016/10/27 Javascript
jquery无法为动态生成的元素添加点击事件的解决方法(推荐)
2016/12/26 Javascript
javascript正则表达式模糊匹配IP地址功能示例
2017/01/06 Javascript
详解vue-cli + webpack 多页面实例应用
2017/04/25 Javascript
JS中数组与对象的遍历方法实例小结
2018/08/14 Javascript
vue项目中使用scss的方法步骤
2019/05/16 Javascript
JS Generator 函数的含义与用法实例总结
2020/04/08 Javascript
python3 pillow生成简单验证码图片的示例
2017/09/19 Python
Python通过matplotlib画双层饼图及环形图简单示例
2017/12/15 Python
python用post访问restful服务接口的方法
2018/12/07 Python
Django框架会话技术实例分析【Cookie与Session】
2019/05/24 Python
django项目简单调取百度翻译接口的方法
2019/08/06 Python
python3 求约数的实例
2019/12/05 Python
Python操作MySQL数据库实例详解【安装、连接、增删改查等】
2020/01/17 Python
tensorflow实现在函数中用tf.Print输出中间值
2020/01/21 Python
Python paramiko 模块浅谈与SSH主要功能模拟解析
2020/02/29 Python
The North Face北面英国官网:美国著名户外品牌
2017/12/13 全球购物
Ajax主要包含了哪些技术
2014/06/12 面试题
英语教学随笔感言
2014/02/20 职场文书
会计专业个人自我鉴定
2014/03/21 职场文书
槐乡的孩子教学反思
2014/04/27 职场文书
安全教育日主题班会
2015/08/13 职场文书
入党申请书怎么写?
2019/06/11 职场文书
PyTorch device与cuda.device用法
2022/04/03 Python