字符串:各种奇葩内置--2017/9/3--2

来源:互联网 发布:配眼镜测得数据的意思 编辑:程序博客网 时间:2024/06/06 01:39

字符串和元组很像

str1='abcd'str1[:1]'ab'

str1[1]
‘b’
字符串不能更改

str2=’biubiu’
capitalize 首字母大写
str2.capitalize()
‘Biubiu’

casefold 全部小写
str2.casefold()
biubiu
capitalize casefold 得到新字符串,strt2不变

count(start[,start[,end) 出现次数
str2.count(xi)
endswith 解释是否为xx
str2.endswith(xi)
startswith 开始是否为xx

expendtabs \t加空格,默认8(a个空格,8-a个空格)
str2.expendtabs(‘B\tiubi\tu’)
‘B iubi u’

find xx是否在字符串中,有-返回索引值,无-返回-1
rfind 右边找

index 与find一致,只是无返回异常
rindex

isalnum >1个字符并且所有均为数字或字符,T

isdigit all数字,T

islower all小写,T
isupper all大写,T

isnumeric all数字字符,T

isspace all空格,T

title 标题化(首字母大写,后面全小写)
istitle 首字母大写,后面全小写,T

join(sub)
str2=’biubiu’
str2.jion(123)
‘1biubiu2biubiu3’
123被隔开

ijust 左对齐

lower 大写变小写
upper

strip 首尾去掉,默认为空格
istrip 去掉左边所有空格
rstrip 右边

partition 找到sub,分成三个字符
str2=’biubiu’
str2.partition(‘iu’)
(‘b’,’iu’,’biu’)
rpartition

replace(old,new[,count) count 最多可替换数

split(sub) 找到sub就切,变成列表;默认为空格

splitlines \n为分隔

swapcase 大小写转换

translate()
str2=’biubiu’
str2.translate(str.maketrans(‘b’,’w’))
‘wiuwiu’

zfill 右对齐,前面用0填充

原创粉丝点击