我试图通过提供 arg_name=VALUE 来尝试字符串替换函数,但得到了TypeError:replace()不接受关键字参数

>>>s =“shubham shriavstava”

>>>s.replace(old=u"sh", new=u"",count=1)

TypeError: replace() takes no keyword arguments

这里出了什么问题?

请您参考如下方法:

您收到错误是因为 str.replace是用 C 实现的内置函数,不能接受关键字参数。来自 Calls section of the docs :

CPython implementation detail: An implementation may provide built-in functions whose positional parameters do not have names, even if they are ‘named’ for the purpose of documentation, and which therefore cannot be supplied by keyword.

您可以通过从函数调用中删除关键字来解决该问题。


评论关闭
IT源码网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!