直接调用没有问题
def test1():
test_str="028-123456"
print test_str.replace(old="-",new="")
def test2():
test_str="028-123456"
print test_str.replace("-","")
test2()
结果
Connected to pydev debugger (build 141.1245)
028123456
Process finished with exit code 0
指出参数名进行调用时出错
Traceback (most recent call last):
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 2357, in <module>
globals = debugger.run(setup['file'], None, None, is_module)
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 1777, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "/Users/function_test/test_builtin_str.py", line 8, in <module>
test1()
File "/Users/function_test/function_test/test_builtin_str.py", line 4, in test1
print test_str.replace(old="-",new="")
TypeError: replace() takes no keyword arguments
没有恶意,但我真的怀疑楼上两位并不太了解Python。
你这么调用在Python中是没问题的,但出现这个问题的真正原因是字符串的
replace
方法不是用Python实现的,而是用C语言实现的,所以它不支持Python里面的keyword
参数特性。你可以试一下用
Python版本的replace
:这个
replace
方法在string
模块中(Lib/string.py文件),是对C语言版本的relace
方法的封装,有兴趣的话你可以去看看它的源码