Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中的String.replace(x,x,count=>;1)_Python_Replace_Syntax - Fatal编程技术网

Python中的String.replace(x,x,count=>;1)

Python中的String.replace(x,x,count=>;1),python,replace,syntax,Python,Replace,Syntax,我是python新手,正在学习它。在我的项目代码中,我在replace函数中看到计数是以这种格式给出的。当我在Python编辑器中运行时,它给出了这个错误 "Bharath..Bharath..Bharath".replace("..", ".", count => 1) SyntaxError:无效语法 在处理上述异常期间,发生了另一个异常: 回溯(最近一次呼叫最后一次): 文件“”,第1行,在 文件“/usr/lib/python3.7/py_compile.py”,第147行,在c

我是python新手,正在学习它。在我的项目代码中,我在replace函数中看到计数是以这种格式给出的。当我在Python编辑器中运行时,它给出了这个错误

"Bharath..Bharath..Bharath".replace("..", ".", count => 1)
SyntaxError:无效语法
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“/usr/lib/python3.7/py_compile.py”,第147行,在compile中
提升py_exc
py_compile.PyCompileError:文件“/prog.py”,第1行
x=“Bharath..Bharath..Bharath.”替换(“..”,“”,计数=>1)
^
SyntaxError:无效语法
有人能告诉我这个计数=>1意味着什么吗?我知道最后一个参数是要替换的事件数


感谢您在Python中使用这非常容易
String.replace(StringToReplace,ReplaceWith,Count)

以你为例

x=“Bharath..Bharath..Bharath.”替换(“..”,“,”,1)
应该可以工作。像“计数”这样的关键词在这里不起作用


因此,您已接近解决方案。:)

Replace方法不接受关键字参数,因此,只需使用以下代码更新代码即可

"Bharath..Bharath..Bharath".replace("..", ".", 1)

只需尝试计数=1或仅1。替换(“…”,“,”,1)