python关于字符串的问题

来源:百度知道 编辑:UC知道 时间:2024/09/23 07:24:20
使用python3.1版
如果要使用find,

a = 'I am hero'
a.find('he', beg=0, end=len(a))

为什么会报错?
Traceback (most recent call last):
File "<pyshell#25>", line 1, in <module>
a.find('he',beg=0,end=len(a))
TypeError: find() takes no keyword arguments

a.find('he', 0, len(a)),这样就好了

字符串好像目前不支持keyword argument

find(...)
S.find(sub [,start [,end]]) -> int

Return the lowest index in S where substring sub is found,
such that sub is contained within s[start,end]. Optional
arguments start and end are interpreted as in slice notation.

Return -1 on failure.

直接用a.find('he') 或者 a.find('he',0,len(a)) 就可以了
不能用beg=0, end=len(a),这里表示给beg和end赋值,可是find参数里没有这两个,find后面那两个是默认的参数,不给也可以的

find无指名参数。

请使用:

a.find('he', 0, len(a))