加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python字符串

发布时间:2020-12-20 10:55:02 所属栏目:Python 来源:网络整理
导读:#查看字符串对象有哪些方法 str1 = ‘admin‘ print(dir(str1)) 【结果】 [‘__add__‘,‘__class__‘,‘__contains__‘,‘__delattr__‘,‘__dir__‘,‘__doc__‘,‘__eq__‘,‘__format__‘,‘__ge__‘,‘__getattribute__‘,‘__getitem__‘,‘__getnewar
#查看字符串对象有哪些方法
str1 = ‘admin‘
print(dir(str1))
【结果】

[‘__add__‘,‘__class__‘,‘__contains__‘,‘__delattr__‘,‘__dir__‘,‘__doc__‘,‘__eq__‘,‘__format__‘,‘__ge__‘,‘__getattribute__‘,‘__getitem__‘,‘__getnewargs__‘,‘__gt__‘,‘__hash__‘,‘__init__‘,‘__init_subclass__‘,‘__iter__‘,‘__le__‘,‘__len__‘,‘__lt__‘,‘__mod__‘,‘__mul__‘,‘__ne__‘,‘__new__‘,‘__reduce__‘,‘__reduce_ex__‘,‘__repr__‘,‘__rmod__‘,‘__rmul__‘,‘__setattr__‘,‘__sizeof__‘,‘__str__‘,‘__subclasshook__‘,‘capitalize‘,‘casefold‘,‘center‘,‘count‘,‘encode‘,‘endswith‘,‘expandtabs‘,‘find‘,‘format‘,‘format_map‘,‘index‘,‘isalnum‘,‘isalpha‘,‘isascii‘,‘isdecimal‘,‘isdigit‘,‘isidentifier‘,‘islower‘,‘isnumeric‘,‘isprintable‘,‘isspace‘,‘istitle‘,‘isupper‘,‘join‘,‘ljust‘,‘lower‘,‘lstrip‘,‘maketrans‘,‘partition‘,‘replace‘,‘rfind‘,‘rindex‘,‘rjust‘,‘rpartition‘,‘rsplit‘,‘rstrip‘,‘split‘,‘splitlines‘,‘startswith‘,‘strip‘,‘swapcase‘,‘title‘,‘translate‘,‘upper‘,‘zfill‘]

带下划线的方法全部忽略,不需要了解

?

#字符串替换
print(str1.replace(‘admin‘,‘baidu‘))
【结果】baidu abc
#索引
print(str1.find(‘m‘))
【结果】2

在接口测试中用得比较多的字符串方法
#字符串是否以...开头
print(str1.startswith(‘a‘))
【结果】True

#字符串以...结果
print(str1.endswith(‘m‘))
【结果】False

#字符串是不是数字
print(str1.isdigit())
【结果】False

#字母大小写切换
print(str1.upper())
print(str1.lower())

#字符串拆分
str2 = ‘hello,little boy‘
str3 = str2.split(‘,‘)
print(str3)
print(type(str3))
【结果】[‘hello‘,‘little boy‘]? ? ? ?<class ‘list‘>
#将列表合并成字符串
str4 = ‘:‘.join(str3)
print(str4)
【结果】hello:little boy

#  %格式化,即用%作为占位符
name = ‘a litte tree‘
age = ‘6‘
print(‘我的名字是%s,我今年%s岁了‘%(name,age))
# format格式化,用{}作为占位符,推荐
print(‘我的名字是{0},我今年{1}岁了‘.format(name,age))
【结果】

我的名字是a litte tree,我今年6岁了我的名字是a litte tree,我今年6岁了

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读