Python字符串中部分字符串的替换

str = 'nin hello world hao hello world'
str1 = 'hello world'
x = len(str1)
str2 = 'hello%20world'
for i in str:
    if str.find(str1) != -1:
        print(str.find(str1))
        str = str.replace(str[str.find(str1):str.find(str1) + x], str2)
print(str)

 

你可能感兴趣的:(Python)