Tutorial Example

Check a String Contains a Substring in Python – Python Tutorial

To check a string contains a substring or not, we can use if… in operation in python.

For example:

Here is a string ‘this is a good night

You want to know ‘good‘ string exists in this string or not, you can use code below.

>>> s = 'this is a good night'
>>> su = 'good'
>>> if su in s:
...     print 'yes'
... else:
...     print 'no'
...
yes

The output is: