How to check if a Python string contains a substring?

Sometimes, we want to check if a Python string contains a substring.

In this article, we’ll look at how to check if a Python string contains a substring.

How to check if a Python string contains a substring?

To check if a Python string contains a substring, we can use the in operator.

For instance, we write:

some_string = 'blah blah'
if "blah" in some_string: 
    print('has blah')

We use the in operator to check if 'blah' is in some_string.

This should return True since it’s in some_string.

Therefore, 'has blah' is logged.

Conclusion

To check if a Python string contains a substring, we can use the in operator.