字符串修改功能_01

1

2

                                                   3

代码展示

#replace() : 修改字符串的内容
# 语法: 字符串序列.replace(旧子串,子新串,替换次数)
# 替换次数如果超出了子串中出现的次数,表示替换所有这个子串,不写替换次数默认全部替换
str1="hello world and itcast and itheima and python"
print(str1.replace("and",'he'))     #hello world he itcast he itheima he python
print(str1.replace("and",'he',5))   #hello world he itcast he itheima he python
print(str1.replace("and",'he',1))   #hello world he itcast and itheima and python
str1.replace("and",'he')
print(str1)                         #hello world and itcast and itheima and python
# 值未变,说明replace()有返回值



#split() : 按照指定字符分割字符串
# 语法: 字符串序列.split(分割字符,num)
# num为分割的次数,最终获得的输出数量为num+1个
# 返回的值为列表类型
str2="hello world and itcast and itheima and python"
print(str2.split("and"))        #['hello world ', ' itcast ', ' itheima ', ' python']
print(str2.split("and",2))      #['hello world ', ' itcast ', ' itheima and python']


# join() :将多个字符串合并成一个新的字符串
# 语法 : 字符或子串.join(多个字符串序列)
l2=['44','55','66']
l3="--".join(l2)
print(l3)       #44--55--66

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

m0_63911789

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值