string.replace()在python 3.x上已弃用。新的方法是什么?
Replace()是python3中<class 'str'>的一个方法:
>>> 'hello, world'.replace(',', ':')
'hello: world'
如2。X,使用str.replace()。
例子:
>>> 'Hello world'.replace('world', 'Guido')
'Hello Guido'
供您参考,当将某些字符附加到字符串中任意位置固定的单词后(例如,通过添加后缀-ly将形容词更改为副词),您可以将后缀放在行尾以提高可读性。要做到这一点,在replace()中使用split():
s="The dog is large small"
ss=s.replace(s.split()[3],s.split()[3]+'ly')
ss
'The dog is largely small'
ss = s.replace(s.split()[1], +s.split()[1] + 'gy')
# should have no plus after the comma --i.e.,
ss = s.replace(s.split()[1], s.split()[1] + 'gy')
python 3中的replace()方法被简单地用于:
a = "This is the island of istanbul"
print (a.replace("is" , "was" , 3))
#3 is the maximum replacement that can be done in the string#
>>> Thwas was the wasland of istanbul
# Last substring 'is' in istanbul is not replaced by was because maximum of 3 has already been reached
你可以使用str.replace()作为str.replace()的一个链。假设你有一个像'Testing PRI/Sec (#434242332;PP:432:133423846,335)'这样的字符串,你想用'-'替换所有的'#',':',';','/'符号。你可以这样替换它(正常方式),
>>> string = 'Testing PRI/Sec (#434242332;PP:432:133423846,335)'
>>> string = string.replace('#', '-')
>>> string = string.replace(':', '-')
>>> string = string.replace(';', '-')
>>> string = string.replace('/', '-')
>>> string
'Testing PRI-Sec (-434242332-PP-432-133423846,335)'
或者这样(str.replace()的链)
>>> string = 'Testing PRI/Sec (#434242332;PP:432:133423846,335)'.replace('#', '-').replace(':', '-').replace(';', '-').replace('/', '-')
>>> string
'Testing PRI-Sec (-434242332-PP-432-133423846,335)'
简单替换:. Replace (old, new, count)。
text = "Apples taste Good."
print(text.replace('Apples', 'Bananas')) # use .replace() on a variable
Bananas taste Good. <---- Output
print("Have a Bad Day!".replace("Bad","Good")) # Use .replace() on a string
Have a Good Day! <----- Output
print("Mom is happy!".replace("Mom","Dad").replace("happy","angry")) #Use many times
Dad is angry! <----- Output
Python 3的str.replace的官方文档
Python 3的str.replace
Str.replace (old, new[, count]) 返回字符串的副本,所有出现的子字符串old都替换为new。如果给出了可选参数count,则只替换第一次出现的计数。
对应的VSCode的语法注意如下:
str.replace(self: str, old, new, count) -> str.replace(self: str, old, new, count
两种方法使用str.replace
方法1:使用内置str的replace -> str.replace(strVariable, old, new[, count])
replacedStr1 = str.replace(originStr, "from", "to")
方法二:使用str变量的replace -> strVariable。替换(old, new[, count])
replacedStr2 = originStr.replace("from", "to")
完整的演示
代码:
originStr = "Hello world"
# Use case 1: use builtin str's replace -> str.replace(strVariable, old, new[, count])
replacedStr1 = str.replace(originStr, "world", "Crifan Li")
print("case 1: %s -> %s" % (originStr, replacedStr1))
# Use case 2: use str variable's replace -> strVariable.replace(old, new[, count])
replacedStr2 = originStr.replace("world", "Crifan Li")
print("case 2: %s -> %s" % (originStr, replacedStr2))
输出:
case 1: Hello world -> Hello Crifan Li
case 2: Hello world -> Hello Crifan Li
截图:
My related (Chinese) post: str.replace String in Python 3
推荐文章
- python:将脚本工作目录更改为脚本自己的目录
- 如何以编程方式获取python.exe位置?
- 如何跳过循环中的迭代?
- 使用Pandas为字符串列中的每个值添加字符串前缀
- ImportError:没有名为matplotlib.pyplot的模块
- 我如何能匹配一个字符串与正则表达式在Bash?
- 在python中遍历对象属性
- 如何在Python中使用方法重载?
- 在Python中提取文件路径(目录)的一部分
- 如何安装没有根访问权限的python模块?
- 尝试模拟datetime.date.today(),但不工作
- 将行添加到数组
- 如何在Python中直接获得字典键作为变量(而不是通过从值搜索)?
- Python:为什么functools。部分有必要吗?
- 如何用python timeit对代码段进行性能测试?