The value of knowledge lies not in possession, but in share.

0%

Python--判断字符串是否包含子字符串

为方便以后开发查阅,特记录此相关代码。
所要实现的功能:

判断字符串是否包含子字符串

方法一:In

1
2
3
4
5
string = 'helloworld'
if 'world' in string:
  print('Exist')
else:
  print('Not exist')

方法二:Find

1
2
3
4
5
string = 'helloworld'
if string.find(’world‘) > -1:
  print('Exist')
else:
  print('Not exist')

方法三:Index

1
2
3
4
if string.index(’world‘) > -1: #因为-1的意思代表没有找到字符,所以判断>-1就代表能找到
  print('Exist')
else:
  print('Not exist')
但是,如果没找到,程序会抛出异常

🍭支持一根棒棒糖吧!