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

2024-04-24 01:09:34 发布

您现在位置:Python中文网/ 问答频道 /正文


Tags: python
3条回答

在2.x中,使用^{}

示例:

>>> 'Hello world'.replace('world', 'Guido')
'Hello Guido'

还要注意,点绑定比字符串连接强,即使用括号:('Hello'+'world').replace('world','Guido')

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

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

>>> 'hello, world'.replace(',', ':')
'hello: world'

相关问题 更多 >