How does python implement the usage and examples of string replacement replace function

Table of contents

1. The syntax and usage of the replace function

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

(2) Usage: Replace the specified string with the target string.

2. Examples

(1) Simple usage

(2) Combined with some functions

①Used in combination with the input function

②Use in combination with input function and if function

③Use in combination with input function, while function and if function


1. The syntax and usage of the replace function

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

old_str: Mandatory, the old string is the target string to be replaced.

new_str: Mandatory, the new string is the target string to be converted.

max_num: optional, specifies the number of substitutions. If not filled, the default is infinite.

(2) Usage: Replace the specified string with the target string.

 kips: The replace function is mainly used in a field column to replace strings with certain rules.


2. Examples

(1) Simple usage

#①
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) Combined with some functions

①Used in combination with the input function

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.'

②Use in combination with input function and if function

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.'

③Use in combination with input function, while function and if function

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'
请输入需要调整的字符串:

#一直循环输入。。。。

reference article 

For specific usage of the input function, please refer to the article: Usage of input function in python

For specific usage of if judgment statement, please refer to: Usage and examples of python's if conditional statement

Other articles: Python replace() method | rookie tutorial (runoob.com)

Guess you like

Origin blog.csdn.net/weixin_50853979/article/details/127329088