简体   繁体   中英

String.replace(x,x,count => 1) in Python

I am new to python and learning it. In my project code I saw in the replace function the count is given in this format. When I run in Python editor it gives this error.

"Bharath..Bharath..Bharath".replace("..", ".", count => 1)
SyntaxError: invalid syntax

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib/python3.7/py_compile.py", line 147, in compile
    raise py_exc
py_compile.PyCompileError:   File "./prog.py", line 1

    x="Bharath..Bharath..Bharath".replace("..", ".", count => 1)

                                                            ^
SyntaxError: invalid syntax

Can anyone tell me what this count => 1 means i understand that the last parameter is the no of occurrences to replace.

Thanks

In Python this can be done very easily String.replace(StringToReplace, ReplaceWith, Count)

For your example

x="Bharath..Bharath..Bharath".replace("..", ".", 1) should work. Keywords like "count" dont work here.

So you were close to the solution. :)

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM