9.13. The in
and not in
operators¶
The in
operator tests if one string is a substring of another:
1
print('p' in 'apple')
2
print('i' in 'apple')
3
print('ap' in 'apple')
4
print('pa' in 'apple')
5
(chp8_in1)
Note that a string is a substring of itself, and the empty string is a substring of any other string. (Also note that computer scientists like to think about these edge cases quite carefully!)
5
1
print('a' in 'a')
2
print('apple' in 'apple')
3
print('' in 'a')
4
print('' in 'apple')
5
(chp8_in2)
The not in
operator returns the logical opposite result of in
.
2
1
print('x' not in 'apple')
2
(chp8_in3)