python中0代表false,为何下面会输入

name = 'Swaroop'
if name.find('Swa')==0:
print'Yes,It does not contain strings "Swa"'

运行后输入内容了,但按语句意思,应该是从name中查找‘Swa’,如果没找到才输出下面的信息,但name中明显包含‘Swa’了,求解释(刚学python,希望讲的通俗易懂点)

虽然我也是初学者,但我解释一下这个问题。
if name.find('Swa')==0的意思是,如果‘Swa’在name里,返回Swa在name中的索引值,这个索引值等于0的话,打印Yes,It does not contain strings "Swa"
如果按你的意思应该这么编:
name = 'Swaroop'
if 'Swa' not in name:
print('Yes,It does not contain strings "Swa"')
else:
print('Yes,It contain strings "Swa"')
温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-08-29
python str.find函数返回的是子字符串开始的下标,所以返回的是0,是正确的。追问

不懂,说的再简单点呢

本回答被提问者和网友采纳
相似回答