字符串方法

字符串方法

#1.capitalize(使字符串的第一字母变成大写,其余为小写)
capitalize_1 = 'a23Adf'
print(1,capitalize_1.capitalize())

#2.casefold(将字符串的字母全部变成小写)
casefold_1 = 'SDF12adAS'
print(2,casefold_1.casefold())

#3.center(width,fillchar):以字符串为中心,将字符串填充成长度为width的新字符串,填充内容为fillchar(默认为空格)
center_1 = 'abc'
print(3,center_1.center(7,'*'))

#4.count(str1,start,end):统计字符串中str1的个数,start表示开始统计的下标位置,end表示统计结束的下标位置。(start,end默认为0和len(字符串)
count_1 = 'how are you? i am fine, thank you. '
print(4,count_1.count('you',9))#从'ou? i am fine, thank you. '往后取

#5.encode():
print('=====================================')

#6.endswith(str1,start,end):检查字符串在范围内是不以str1结束,start和end表示开始到结束的下标位置(默认为0,len(字符串)
endswith_1 = 'how are you? i am fine, thank you.'
print(6,endswith_1.endswith('fine',0,-12))

#7.expandtabs(number):将字符串中的制表符(\t、tab键)换成number个空格(默认为8个空格)
expandtabs_1 = '将字符串中的制表符(\t、tab键)换成number\t个空格(默认为8个空格)'
print(7,expandtabs_1.expandtabs(2))

#8.find(str1,start,end):找到字符串在范围内第一个str1出现的下标位置,start和end表示开始和结束的范围(默认为0,len(字符串))
find_1 = '找到字符串在范围内第一个str1出现的下标位置,start和end表示开始和结束的范围(默认为0,len(字符串))'
print(8,find_1.find('字符串'))

#9.format():格式化字符串,用{}和:代替%的功能
format_1 = 'how are {}? {} am fine, thank {}.'
print(9,format_1.format('you','i','you'))
print('how are {0}? {1} am fine, thank {0}.'.format('you','i'))
print('how are {one}? {two} am fine, thank {one}.'.format(**{'one':'you','two':'i'}))#用字典格式化,必须加**
print('how are {0[0]}? {0[1]} am fine, thank {0[0]}.'.format(['you','i']))#用列表来格式话,必须加0

#10.format_map()
print(10,'========================')

#11.index(str1,start,end):和find的功能一样,但是str1不在字符串中会报错
index_1 = '找到字符串在范围内第一个str1出现的下标位置,start和end表示开始和结束的范围(默认为0,len(字符串))'
print(11,index_1.index('字符串'))

#12.isalnum():检查字符串是不是只有数字或者字母
isalnum_1 = 'A5548-df'
print(12,isalnum_1.isalnum())

#13.isalpha():检查字符串是不是所有字符都是字母
isalpha_1 = 'sdfAW'
print(13,isalpha_1.isalpha())

#14.isascii():检查字符串是不是为空字符串或者为ASCII码(ASCII码是从U0000~u007f之间),也就是字母数字英式符号等
isascii_1 = 'x>.'
print(14,isascii_1.isascii())


#15.isdecimal():判断字符串是不是只包含十进制数字
isdecimal_1 = '06556'
print(15,isdecimal_1.isdecimal())

#16.isdigit():判断字符串是不是由纯数字组成
isdigit_1  = '564560'
print(16,isdigit_1.isdigit())
#清楚isdigit和isdecimal的区别

#17.isidentifier():判断字符串是不是有效的标识符,主要用来判断变量名是否可用
isidentifier_1 = '1一壹1wef'
print(17,isidentifier_1.isidentifier())

#18.islower():判断字符串的字母是不是全部小写,并且至少有一个小写字母
islower_1 = 'dsf./ae55'
print(18,islower_1.islower())

#19.isnumeric():# 判断字符串是不是由纯数字字符组成(包括大写数字,和中文数字)
isnumeric_1 = '一壹1'
print(19,isnumeric_1.isnumeric())

#20.isprintable():检查字符串是否可以打印(不可打印的是\t和\n)
isprintable_1 = '\t'
print(20,isprintable_1.isprintable())

#21.isspace():检查字符串是不是有空格
isspace_1 = '  \t\n'
print(21,isspace_1.isspace())

#22.istitle():判断字符串是不是标题化的,并且至少有一个大写字母(每个英语单语首字母大写,其余字母小写,也就是每个以字母以外的字符隔开的字母字符串是不是以字母大写开头),
istitle_1 = 'Sd54Ed,Sg Sf'
print(22,istitle_1.istitle())

#23.isupper():判断字符串的字母是不是全部大写,且必须有至少一个大写字母
isupper_1 = '水电费SDF12'
print(23,isupper_1.isupper())

#24.str1.join(字符串序列):用str1将字符串序列连接在一起
join_1 = ['y','o','u']
print(24,''.join(join_1))

#25.ljust(width,fillchar):将字符串左对齐,填充成长度为width的新字符串。
ljust_1 = 'abc'
print(25,ljust_1.ljust(8,'+'))

#26.rjust(width.fillchar):将字符串右对齐,填充成长度为width的新字符串。
rjust_1 = 'abc'
print(26,rjust_1.rjust(8,'+'))

#27.lstrip(str1):截掉字符串左边的str1(默认为空格)
lstrip_1 = '+++abc+++'
print(27,lstrip_1.lstrip('+'))

#28.rstrip(str1):截掉字符串右边的str1(默认为空格)
rstrip_1 = '+++abc+++'
print(28,rstrip_1.rstrip('+'))

#29.strip(str1):截掉字符串两边的str1,不能截掉中间的字符(默认为空格)
strip_1 = '+++abc+++'
print(29,strip_1.strip('+'))

#30.maketrans(str1,str2):创建字符映射的转换表,在字符串中将str1中的字符对应转换成str2对应的字符
str_1 = '12345'
str_2 = '一二三四五'
trantab = str.maketrans(str_1,str_2)
print(30,'s355ef12'.translate(trantab))#maketrans创建字符映射表和trantab搭配使用

#31.partition(str1):将字符串在str1出现的第一个位置,将字符串以str1以为中心切割成,字符串左边和str1,以及字符串右边的元组
partition_1 = 'how are you? i am fine ,thank you.'
print(31,partition_1.partition('you'))

#32.replace(str1,str2,N):将字符串中的前面N个str1换成str2。
replace_1 = 'how are you? i am fine ,thank you.'
print(32,replace_1.replace('you','i',1))

#rfind(str1,start,end):查找字符串在范围内中str1出现的最后一个位置,start和end表示范围(默认为0或者len(字符串)),如果str1不在字符串中结果为-1。
rfind_1 = 'how are you? i am fine ,thank you.'
print(33,rfind_1.rfind('you',0,-2))

#34rindex(str1,start,end):查找字符串在范围内中str1出现的最后一个位置,start和end表示范围(默认为0或者len(字符串)),如果str1不在字符串中,系统会报错。
rindex_1 = 'how are you? i am fine ,thank you.'
print(34,rindex_1.rindex('you',0,-2))

#35.rpartition(str1):将字符串在str1出现的最后一个位置,将字符串以str1以为中心切割成,字符串左边和str1,以及字符串右边的元组
rpartition_1 = 'how are you? i am fine ,thank you.'
print(35,rpartition_1.partition('i'))

#36.split(str1,N):将字符串以str1为切割点切割到第N个切割点(N默认为全部)
split_1 = 'how are you? i am fine ,thank you.'
print(36,split_1.split('you',1))

#37.rsplit(str1,N):将字符串以从右到左的str1为切割点切割到第N个切割点(N默认为全部)
rsplit_1 = 'how are you? i am fine ,thank you.'
print(37,rsplit_1.rsplit('you',1))

#38.splitlines():将字符串中的换行符为切割点,切割成列表(默认为False,不保留换行符,改成True,则保留换行符)
splitlines_1 = 'how \nare you?\r i am \nfine ,thank you.'
print(38,splitlines_1.splitlines(True))

#39.startswith(str1,start,end):判断字符串在范围内是不是以str1为开头,start和end表示范围(默认为0或者len(字符串))
startswith_1 = 'how are you? i am fine ,thank you.'
print(39,startswith_1.startswith('you',8))

#40.swapcase():将字符串中的大写换成小写,小写换成大写
swapcase_1 = 'AD15sdf'
print(40,swapcase_1.swapcase())

#41.title():将字符串中的字符标题化,并且至少有一个大写字母(每个英语单语首字母大写,其余字母小写,
# 也就是每个以字母以外的字符隔开的字母字符串是不是以字母大写开头)
title_1 = 'sd_er sd1et'
print(41,title_1.title())

#42.translate(table):通过maketrans转换的映射表将字符串中的字符转换。
str_1 = '12345'
str_2 = '一二三四五'
translate_1 = str.maketrans(str_1,str_2)
print(30,'s355ef12'.translate(translate_1))

#43.upper():将字符串中的字母全部大写
upper_1 = 'sdfAsd12'
print(43,upper_1.upper())

#44.zfill():将字符串右对齐,用0填充成width长度的字符串
zfill_1 = 'abc'
print(44,zfill_1.zfill(8))

#45.max():求序列中的最大值(按照编程码比较大小)
max_1 = 'zeasdf'
print(45,max(max_1))

#46.min():求序列中的最小值(按照编程码比较大小)
min_1 = 'zeasdf'
print(46,min(max_1))

#47.len():计算出序列中的元素个数
len_1 = 'sdfgega56f5s'
print(47,len(len_1))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值