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

Dew*_*rld 249 python python-3.x

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

Ign*_*ams 281

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

例:

>>> 'Hello world'.replace('world', 'Guido')
'Hello Guido'
Run Code Online (Sandbox Code Playgroud)

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

  • 如果我必须使用堆栈溢出来浏览Python文档,那么显然存在问题.如果您正在阅读Python团队.梳理出来! (30认同)
  • @ToolmakerSteve:不推荐使用`string`函数.`str`方法不是. (7认同)
  • FWIW,每当我谷歌,我似乎最终在旧的已弃用的字符串函数.以下是(未弃用)字符串方法的链接.http://docs.python.org/3.3/library/stdtypes.html#string-methods~or_for_2~http://docs.python.org/2/library/stdtypes.html#string-methods (6认同)
  • "re"(正则表达式)模块可以替代(某些?所有?)不推荐使用的字符串函数.在这种情况下,`re.sub()`. (4认同)
  • 在对象而不是类上调用方法。`'foo'.replace(...)` (2认同)

kev*_*kev 103

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

>>> 'hello, world'.replace(',', ':')
'hello: world'
Run Code Online (Sandbox Code Playgroud)


小智 11

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
Run Code Online (Sandbox Code Playgroud)

  • 请记住,你也不能放 3,它会改变所有的巧合。 (3认同)

Kus*_*era 9

您可以使用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)'
Run Code Online (Sandbox Code Playgroud)

或者这样(str.replace()链)

>>> string = 'Testing PRI/Sec (#434242332;PP:432:133423846,335)'.replace('#', '-').replace(':', '-').replace(';', '-').replace('/', '-')
>>> string
'Testing PRI-Sec (-434242332-PP-432-133423846,335)'
Run Code Online (Sandbox Code Playgroud)


cri*_*fan 8

str.replace的官方文档Python 3

\n

官方文档:Python 3str.replace

\n
\n

str.replace(旧的,新的[,计数])

\n

返回字符串的副本,其中所有出现的子字符串 old 都被 new 替换。如果给出了可选参数 count,则仅替换第一个出现的 count。

\n
\n

相应的VSCode语法提示是:

\n

在此输入图像描述

\n
\n

str.replace(self: str, 旧的, 新的, 计数) -> str

\n
\n

两种使用方法str.replace

\n
    \n
  • 方法1:使用内置str\的replace ->str.replace(strVariable, old, new[, count])
  • \n
\n
replacedStr1 = str.replace(originStr, "from", "to")\n
Run Code Online (Sandbox Code Playgroud)\n
    \n
  • 方法2:使用str变量替换->strVariable.replace(old, new[, count])
  • \n
\n
replacedStr2 = originStr.replace("from", "to")\n
Run Code Online (Sandbox Code Playgroud)\n

完整演示

\n

代码:

\n
originStr = "Hello world"\n\n# Use case 1: use builtin str\'s replace -> str.replace(strVariable, old, new[, count])\nreplacedStr1 = str.replace(originStr, "world", "Crifan Li")\nprint("case 1: %s -> %s" % (originStr, replacedStr1))\n\n# Use case 2: use str variable\'s replace -> strVariable.replace(old, new[, count])\nreplacedStr2 = originStr.replace("world", "Crifan Li")\nprint("case 2: %s -> %s" % (originStr, replacedStr2))\n
Run Code Online (Sandbox Code Playgroud)\n

输出:

\n
case 1: Hello world -> Hello Crifan Li\ncase 2: Hello world -> Hello Crifan Li\n
Run Code Online (Sandbox Code Playgroud)\n

截屏:

\n

在此输入图像描述

\n

我的相关(中文)帖子:\xe3\x80\x90\xe8\xaf\xa6\xe8\xa7\xa3\xe3\x80\x91Python 3\xe4\xb8\xad\xe5\xad\x97\xe7\xac\xa6 \xe4\xb8\xb2\xe7\x9a\x84\xe6\x9b\xbf\xe6\x8d\xa2str.replace

\n


小智 6

尝试这个:

mystring = "This Is A String"
print(mystring.replace("String","Text"))
Run Code Online (Sandbox Code Playgroud)