Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Check if a Python String Contains a Substring (Overview)

If you’re new to programming or come from a programming language other than Python, you may be looking for the best way to check whether a string contains another string in Python.

Identifying such substrings comes in handy when you’re working with text content from a file or after you’ve received user input. You may want to perform different actions in your program depending on whether a substring is present or not.

In this video course, you’ll focus on the most Pythonic way to tackle this task, using the membership operator in. Additionally, you’ll learn how to identify the right string methods for related, but different, use cases.

Finally, you’ll also learn how to find substrings in pandas columns. This is helpful if you need to search through data from a CSV file.

Download

Sample Code (ZIP)

21.9 KB
Download

Course Slides (PDF)

8.9 MB

00:00 Hello, and welcome to this course on how to check if a Python string contains a substring. So if you’re working with text data in Python, then you might have run into a situation where you just need to confirm whether there is a certain substring present in a text.

00:15 If the text is pretty long, then this might get challenging by just, you know, looking at it. Is the word secret in this long text? Kind of hard to say.

00:25 But together with Python, it’s actually quite quick to do that. And in this course, you’ll learn how to check for a substring in a string. So this is just confirming the presence of a substring, and that’s quite quick and easy, and I’ll show you the most Pythonic way to do that.

00:41 But in good Real Python fashion, we’ll dive much deeper than that. As the next point, I’m going to show you how you can generalize the check by removing case sensitivity from your string, and also give you a bit of context on why that is important.

00:54 Then you’ll learn about a few string methods that you can use to figure out more about the substring, such as where it’s located in your text and also how many of those substrings exist.

01:07 Then I’ll show you some more advanced ways of matching substrings using regular expressions, so situations where you might need to apply more elaborate conditions in order to find just the substring that you need, and then also work with it more.

01:21 And finally, you’ll also take a look at how you can find a substring in a pandas DataFrame column. This, again, is going to be a different approach than the ones shown before, and that’s just because you want to do things the way that pandas suggests you to do them. So there’s quite a lot of topics, and after going through this course, you’re going to have a good foundation on working with text data in Python and how to check for the presence of a substring, but then also how to work more with the substring if you found it.

01:50 If that’s something you’re interested in, then stick with me, and let’s get over to the next lesson, where we’ll start with the quick takeaway of how you can confirm the presence of a substring using Python.

Become a Member to join the conversation.