派森学长带你学python—字符串(5)

一.字符串的拼接

1.' '.join([])

2.直接拼接

3.格式化字符串进行拼接 

str1='hello'
str2='world'
print(str1+str2)#(0)运算符拼接
print(''.join(['hello','world','I','love','China']))#(1)使用空字符串进行拼接
#helloworldIloveChina
print('+'.join(['hello','world']))#hello+world
print('hello''world')#(2)直接拼接
#(3)格式化字符串进行拼接
print('%s%s' % (str1,str2))#helloworld
print(f'{str1}{str2}')#helloworld
print('{0}{1}'.format(str1,str2))#helloworld

二.字符串的去重

#1.字符串加拼接方法
s='helloworldoo'
snew=''
for item in s:
    if item not in snew:
        snew+=item
print(snew)#helowrd
#2.索引加not in方法
snew1=''
for i in range(len(s)):
    if s[i] not in snew1:
        snew1+=s[i]
print(snew1)#helowrd
#3.集合去重+列表排序
snew2=set(s)
lst=list(snew2)
lst.sort(key=s.index)
print(''.join(lst))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

派森学长

让我们一起学习python

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值