Python的replace函数
Python的replace函数是用于替换字符串中的指定子串的方法。它可以在字符串中找到指定的子串,并将其替换为新的字符串。
语法:
str.replace(old, new[, count])
参数说明:
- old:表示需要被替换的字符串。
- new:表示用来替换old的新字符串。
- count:表示替换的次数。如果指定了count,则只替换前count次出现的old字符串。
示例:
s = "hello world" s = s.replace("world", "Python") print(s) # 输出:hello Python
s = "hello world, world, world" s = s.replace("world", "Python", 2) # 只替换前两个 print(s) # 输出:hello Python, Python, world
原文地址: https://www.cveoy.top/t/topic/Xcg 著作权归作者所有。请勿转载和采集!