python如何实现字符串替代replace函数的用法和实例

目录

1.replace函数的语法和用法

(1)语法:str.replace(old_str,new_str[,max_num])

(2)用法:将指定字符串替代为目标字符串。

2.实例

(1)简单的用法

(2)与一些函数结合使用

①与input函数结合使用

②与input函数和if函数结合使用

③与input函数、while函数和if函数结合使用


1.replace函数的语法和用法

(1)语法:str.replace(old_str,new_str[,max_num])

old_str:必选,旧的字符串,就是被替代的目标字符串。

new_str:必选,新的字符串,就是转为的目标字符串。

max_num:可选,指定替代次数。不填,默认为无数次。

(2)用法:将指定字符串替代为目标字符串。

 kips:该replace函数主要运用在某字段列中,替代具有一定规律的字符串。


2.实例

(1)简单的用法

#①
string='Hello Amy,welcome to our world'
new_string = string.replace('our','my')
new_string
#输出结果:'Hello Amy,welcome to my world'

#②
string1='Hello Amy,welcome to my world,your world,his world,her world,our world'
new_string1 = string1.replace('world','place')
new_string1
#输出结果:'Hello Amy,welcome to my place,your place,his place,her place,our place'


#③
string2='Hello Amy,welcome to my world,your world,his world,her world,our world'
new_string2 = string1.replace('world','place',4)
new_string2
#输出结果:'Hello Amy,welcome to my place,your place,his place,her place,our world'

#解释:①和②例子第三个参数默认为无数个,即可以替代无数个,③设置次数表示只能替代4个

(2)与一些函数结合使用

①与input函数结合使用

old_str = 'I'
new_str = 'all'
string = input('请输入需要调整的字符串:')
new_string = string.replace(old_str,new_str)
print(new_string)

#iput:
'Forever young,I want to be forever young.Forever young,I want to be forever young.So many dreams swinging out of blue.We let them come true.'

#output:
'Forever young,all want to be forever young.Forever young,all want to be forever young.So many dreams swinging out of blue.We let them come true.'

②与input函数和if函数结合使用

old_str = 'I'
new_str = 'all'
string = input('请输入需要调整的字符串:')
if 'I' in string:
    new_string = string.replace(old_str, new_str)
    print(new_string)
elif 'We'in string:
    new_string = string.replace('We', 'all of us')
    print(new_string)
else:
     new_string = string.replace('We', 'I')
     print(new_string)



#input:
请输入需要调整的字符串:'Forever young,I want to be forever young.Forever young,I want to be forever young.So many dreams swinging out of blue.We let them come true.'

#output:
'Forever young,all want to be forever young.Forever young,all want to be forever young.So many dreams swinging out of blue.We let them come true.'

③与input函数、while函数和if函数结合使用

old_str = 'I'
new_str = 'all'
while True:
    string = input('请输入需要调整的字符串:')
    if 'I' in string:
        new_string = string.replace(old_str, new_str)
        print(new_string)
    elif 'We'in string:
        new_string = string.replace('We', 'all of us')
        print(new_string)
    else:
         new_string = string.replace('We', 'I')
         print(new_string)

#①第一个循环
请输入需要调整的字符串:'Forever young,I want to be forever young.Forever young,I want to be forever young.So many dreams swinging out of blue.We let them come true.'
#输出结果为:
'Forever young,all want to be forever young.Forever young,all want to be forever young.So many dreams swinging out of blue.We let them come true.'


#②第二个循环
请输入需要调整的字符串:'I want to go into your heart,let me live into there'
#输出结果:
'all want to go into your heart,let me live into there'
请输入需要调整的字符串:

#一直循环输入。。。。

参考文章 

具体input函数用法可参考文章:python的input函数用法_小白修炼晋级中的博客-CSDN博客_python中input的用法

具体if判断语句用法可参考:python的if条件语句的用法及实例_小白修炼晋级中的博客-CSDN博客_python的if条件

其他文章:Python replace()方法 | 菜鸟教程 (runoob.com)

猜你喜欢

转载自blog.csdn.net/weixin_50853979/article/details/127329088
今日推荐