python判断字符串是否包含⼦字符串第⼀种⽅法:in
string = 'helloworld'
if 'world' in string:
  print 'Exist'
else:
  print 'Not exist'
第⼆种⽅法:find
子字符串是什么
string = 'helloworld'
if string.find(’world‘) == 5: #5的意思是world字符从那个序开始,因为w位于第六个,及序为5,所以判断5
  print 'Exist'
else:
  print 'Not exist'
第三种⽅法:index,此⽅法与find作⽤类似,也是到字符起始的序号
if string.index(’world‘) > -1: #因为-1的意思代表没有到字符,所以判断>-1就代表能到
  print 'Exist'
如果没到,程序会抛出异常

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。