Check a String Contains a Substring in Python – Python Tutorial

By | August 13, 2019

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:

python string contains a substring

Leave a Reply

Your email address will not be published. Required fields are marked *