首页 > 编程语言 > 详细

Python字符串常用方法

时间:2020-12-30 11:18:24      阅读:34      评论:0      收藏:0      [点我收藏+]

Python提供了很多对字符串的操作的方法,罗列经常使用到的方法:

str=Hello
#首字母变大写
print str.capitalize()
#内容居中
print str.center(30,=)
#子序列的个数(字母在字符串中出现了几次)
print str.count(l)
#是否已什么结尾
print str.endswith(o)
#是否已什么开始
print str.startswith(H)
#处理tab键
str2=Hello\t999
print str2.expandtabs()
#寻找子序列位置,没有找到返回-1,返回了是1
print str.find(a)
#寻找子序列的位置,没有找到就报错
print str.index(e)
#字符串的格式化输出,也就是占位符
age=20
name=wuya
print name is {0},and age is {1}.format(name,age)
#判断是否是字母和数字
print str.isalnum()
#判断是否是字母
print str.isalpha()
#判断是否是数字
print str.isdigit()
#判断是否小写
print str.islower()
#判断是否有空格
print str.isspace()
#判断是否是大写
print str.isupper()
#判断是否是标题-->首字母大写,可以理解为大写
print str.istitle()
#.join连接字符串
s1=[appium,selenium,android,ios]
print ***.join(s1)
#使用.join()把列表转为字符串
print ,.join(s1)
#字符串转为列表
a=a b c
print a.split( )
#移除空白
s3= hello
print s3.strip()
#移除左側空格
s4= hello
print s4.lstrip()
#移除右边空格
s5=world 
print s5.rstrip()
#字符串变小写
print str.lower()
#分割字符串,分割后就是元组
s=wuya is python
print s.partition(is)
#替换字符串
print s.replace(wuya,selenium)
#rfind()从右向左找
print s.rfind(wuya)
#bytes可以把字符串转成字节
str5=无涯
print bytes(str5)

 

Python字符串常用方法

原文:https://www.cnblogs.com/liruxian/p/14210138.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!