首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在Python3.x中使用string.replace()

如何在Python3.x中使用string.replace()
EN

Stack Overflow用户
提问于 2012-02-26 17:50:21
回答 6查看 526.3K关注 0票数 339

string.replace()在Python3.x上已弃用。这样做的新方法是什么?

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2012-02-26 17:53:44

与在2.x中一样,使用str.replace()

示例:

代码语言:javascript
复制
>>> 'Hello world'.replace('world', 'Guido')
'Hello Guido'
票数 371
EN

Stack Overflow用户

发布于 2012-02-26 17:53:27

replace()是python3中<class 'str'>的一种方法:

代码语言:javascript
复制
>>> 'hello, world'.replace(',', ':')
'hello: world'
票数 120
EN

Stack Overflow用户

发布于 2017-04-24 23:09:21

python 3中的replace()方法只需通过以下方式使用:

代码语言:javascript
复制
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
票数 20
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9452108

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档