Python字符串学习(四)

字符串中常用的]方法

字符串中有7个自带的方法是常用的:find()、join()、split()、strip()、upper()、lower()、replace()

方法1:find()

find()方法是从前往后找,找到第一个指定的字符串并返回其索引值,否则返回-1

test = 'qerloet4'
v1 = test.find('e')
v2 = test.find('9')
print(v1,v2)
#输出结果 1 -1

方法2:join()

join()方法是将字符串每一个元素按照指点分隔符进行拼接

test = 'hello'
t = '+'
v = t.join(test)
print(v)
#输出  h+e+l+l+o

方法3: split()

split()方法是可以指定字符来分割字符串,可以指定个数,但是取不到指定的字符集分割元素

test = 'testasdsddf'
v = test.split('s')
print(v)

#按s进行分割,分割次数为1次
test = 'testasdsddf'
v = test.split('s',1)
print(v)

#按s进行分割,分割次数为两次
test = 'testasdsddf'
v = test.split('s',2)
print(v)

#输出结果 
['te', 'ta', 'd', 'ddf']
['te', 'tasdsddf']
['te', 'ta', 'dsddf']

splitlines()分割只能根据换行符来进行分割

test = 'asdf\n1234\njklda\n'
print(test)
v = test.splitlines()
print(v)
v1 = test.splitlines(True)#加True分割结果显示\n
print(v1)
v2 = test.splitlines(False)#加False分割结果显示\n
print(v2)
#输出结果
'''
asdf
1234
jklda

['asdf', '1234', 'jklda']
['asdf\n', '1234\n', 'jklda\n']
['asdf', '1234', 'jklda']
'''

方法4:strip()

strip()方法是默认去除空格和/n,/t也可以去除指定字符,同时strip()也有两个衍生方法:lstrip()和rstrip()它俩分别是去除左面和右面的字符默认为空格是

test = '    asf    '
v = test.lstrip()
v1 = test.rstrip()
v2 = test.strip()
print(v,v1,v2)
#输出结果 asf         asf asf

移除指定字符,优先最多匹配

test = 'alex'
v =  test.strip('a')
print(v)
test = 'xaexlex'
v1 = test.rstrip('qlexex')
#从xaexlex右面开始匹配,先除去lex然后在除去ex,最后的结果是xa
print(v1)
#输出结果 lex   xa

方法5:lower()和islower()

lower()方法是把大写应为都变为小写
islower()方式是判断字符串是否都为小字母

test = 'ASdf'
v = test.lower()
print(v)
test1 = 'Beijing'
test2 = 'shanghai'
v1 = test1.islower()
v2 = test2.islower()
print(v1)
print(v2)
#输出结果 
'''
asdf
False
True
'''

方法6:upper()和isupper()

upper()和isupper()方法正好和lower()和islower()方法相反,是把字母变为大写和判断字母是否都为大写

test = 'Asdg'
test1 = 'dffg'
test2 = 'BEI'
test3 = 'Shanghai'
v = test.upper()
v1 = test1.upper()
v2 = test2.isupper()
v3 = test3.isupper()
print(v,v1,v2,v3)
#输出结果 ASDG DFFG True False

方法7:replace()

replace函数可以把字符串里面的old字符串替换成new字符串,max参数指替换不超过max次

test = 'i am xm xm xm xm xm xm'
v = test.replace('xm','tt',2)
print(v)
#输出结果 i am tt tt xm xm xm xm

replace(old,new,max)

方法8:capitalize()

capitalize()方法是将字符串的首字母大写

test = 'hello'
v = test.capitalize()
print(v)
#输出  Hello

方法9:casefold()

casefold()和lower()一样时间字符串中的大写字母变为小写,但是lower()只能将大写的英文字母变为小写而casefold()对应的语言更广

test = 'hEllo'
v = test.casefold()
print(v)
#输出  hello

方法10:center()

center()方法是将字符串前后添加指定数量的拼接符,拼接符默认为空格

test = 'hEllo'
v = test.center(20)
v1 = test.center(20,'k')
v2 = test.center(20,'+')
print(v,v1,v2)
#输出        hEllo         kkkkkkkhEllokkkkkkkk +++++++hEllo++++++++

方法11:count()

count()方法是统计指定字符出现的geshu
def count(self,sub,start=None,end=None)

test = 'qwereuuuue'
v9 = test.count('e')
print(v9) #---->3
v10 = test.count('e',3) #统计从下角标为3的位置开始统计 e 出现的次数
print(v10) #--->2
v11 = test.count('e',1,6)#统计从下角标为3的位置开始到下角标为6的位置结束统计 e 出现的次数
print(v11) #---->2

方法12:endswith()

endswith()判断字符串是否已指定字符结尾

t = 'qwer'
v = t.endswith('a')
v1 = t.endswith('r')
print(v,v1)
#输出结果 False True

方法13:startswith()

satrtswith()方法是endswith()的反例,是判断点字符串是否已指定字符开头

方法14:format()

format()方法是将也字符串中的占位符替换为指定的值

test = 'i am {name},age{nl}'
print(test)
v = test.format(name='小明',nl=22)
print(v)
#输出结果:
# i am {name},age{nl}
# i am 小明,age22

format()也可以按位置进行格式化

test = 'i am {1},age{0}'
print(test)
v = test.format('小明',22)
print(v)
#输出结果
#i am {0},age{0}
# i am 22,age小明

方法15:format_map()

format_map()的用法和format()用法是一样,区别在于format_map是以字典的形式来进行格式化的


```python
test = 'i am {name},age {nl}'
v = test.format(name='小明',nl=22)
v1 = test.format_map({
     'name':'小张','nl':20})
print(v,v1)
#输出结果  i am 小明,age 22       i am 小张,age 20

方法16:index()

index()方法和find是一样都是找指定字符在字符串中的所有,不同的是find()在找不到时不会报错会返回 -1,而index()会报错

test = 'dafafad'
v = test.index('d')
print(v)
#输出结果 0
test = 'dafafad'
v1 = test.index('o')
print(v1)
#输出结果 会提示ValueError: substring not found

方法17:isalunm()

isalunm()方法是判断字符串是不是由字母和数字组成,可以是字母/数字/字母+数据

test = 'uio'
test1 = '9087'
test2 = '890uio'
test3 = '90-iop_'
v = test.isalnum()
v1 = test1.isalnum()
v2 = test2.isalnum()
v3 = test3.isalnum()
print(v,v1,v2,v3)
#输出结果 True True True False

方法18:isalpha()

isalpha()方法是判断字符串是否是字母组成的

test = 'jkl'
v = test.isalpha()
print(v)
#输出结果 Ture

方法19:isdecimal()和isdigit()

isdecimal()和isdigit()方法是判断字符串是否都是数字组成的
isdigit()方法比较特殊,它可以支持比较特殊的数字如 ⑴ ,但是不支持中文的数字如 一, 二这样的

test = '1234'
test2 = 'fjad2'
v5 = test.isdecimal()
v6 = test2.isdigit() #可以支持特殊的数字,但是不支持中文数字如 三 四
print(v5,v6) #Ture False

特殊数字

test = '⑴'
v = test.isdigit()
print(v)
#输出结果 Ture

方法20:isnumeric()

isnumeric()方法是判断值是否都是数字和方法19中的功能类似,区别是isnumeric()支持中文数字

test = '二'
v = test.isnumeric()
print(v)
#输出结果 Tuer

方法21:isspace()

isspace()方法是判断字符串中抒发都不是空格,全是空格就是Ture,反之为False

test = ' '
test1 = 'fda f'
v = test.isspace()
v1 = test1.isspace()
print(v,v1)
#输出结果 True False

方法22:ljust()

ljust()方法是返回一个原字符串左对齐,并使用空格填充指定长度也可以指定填充的符号,若有指定的长度小于原长度就返回原字符串

test = 'asdf'
v = test.ljust(20)
print(v)
v1 = test.ljust(20,'-')
print(v1)
#输出结果
#asdf                
#asdf----------------

方法23:rjust()

rjust()方法和上面ljust()方法正好是相反的效果

方法24:zfill()

zfill()方法其实就是有对齐并用 0 填充指定长度

test = 'daf'
v = test.zfill(5)
print(v)
#输出结果 00daf

方法25:maketrans()和translate()

maketrans()和translate()一般是说在一起用的,maketrans()就是把两个字符串关系对应起来,translate()是将对应起来的字符串进行替换,如下

test = 'ahjkmkhhr;jk'
v = str.maketrans('am;r','1234')
new_test = test.translate(v)
print(new_test)
#输出结果  1hjk2khh43jk

方法26:partition()和rpartition()

partition()和rpartition()方法是用来做分割用的,partition()是将字符串按指定字符进行分割,如字符串中存在多个指定字符则按第一个字符出现的位置进行分割,rpartition()是从字符串的右面开始,顺序和pratition()是相反的

test = 'testsatsuu'
v = test.partition('s')
print(v)
#输出结果 ('te', 's', 'tsatsuu')
test = 'testsatsuu'
v = test.rpartition('s')
print(v)
#输出结果 ('testsat', 's', 'uu')

方法27:swapace()

swapace()将大小写转换即将小写—>大写,大写—>小写

test = 'AsdF'
v = test.swapcase()
print(v)
#输出结果 aSDf

你可能感兴趣的:(python,字符串)