Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Check if a Python String Contains a Substring (Summary)

Like a persistent treasure hunter, you found each "secret", no matter how well it was hidden! In the process, you learned that the best way to check whether a string contains a substring in Python is to use the in membership operator.

You also learned how to descriptively use two other string methods, which are often misused to check for substrings:

  • .count() to count the occurrences of a substring in a string
  • .index() to get the index position of the beginning of the substring

After that, you explored how to find substrings according to more advanced conditions with regular expressions and a few functions in Python’s re module.

Finally, you also learned how you can use the DataFrame method .str.contains() to check which entries in a pandas DataFrame contain a substring .

You now know how to pick the most idiomatic approach when you’re working with substrings in Python. Keep using the most descriptive method for the job, and you’ll write code that’s delightful to read and quick for others to understand.

To continue your learning journey, check out these video courses:

You might also enjoy these written tutorials:

Download

Sample Code (ZIP)

21.9 KB
Download

Course Slides (PDF)

8.9 MB

00:00 And that’s a wrap. You’re at the end of How to Check if a Python String Contains a Substring. And you’ve learned quite a lot in this course.

00:08 Now you do have the skills to figure out whether there is actually the word "secret" in this long text, and you can download the slides for this course in the supporting materials and then actually copy/paste this text.

00:20 So this is a little task that I want to send you off with. Take this long text and then apply the different ways that you learned to identify a substring in a text and figure out whether the word "secret" appears in here.

00:34 And these are the topics that you covered in this course. First, you learned how to confirm the presence of a substring and that the best way to do this in Python is using the in operator.

00:43 Then you learned how you can generalize that check by removing the case sensitivity, and you did that by lowercasing, the string that you’re searching in. Next, you’ve worked with a couple of string methods that you can use to learn more about the substring, such as using .count() to figure out how often does it appear in the text or using .index() to figure out the location of the substring.

01:06 And that led us over to a situation where maybe you want to work more with the substring or you want to apply some conditions to the search. And both of that works well when you’re using regular expressions with the re module. And you’ve seen a couple of examples of how to do that. And finally, you also learned how you can confirm the presence of a substring inside of a pandas DataFrame column and look at the pandas way of doing that by using .str.contains(). That’s all for this course.

01:37 There’s a couple of resources that I already shared, and here’s some other articles and courses where you can continue your learning. So the first one is called Strings and Character Data in Python.

01:47 That exists as a tutorial as well as a course, if you want to learn more about strings in general. Then we have all the resources on regular expressions and building regexes in Python. There’s a tutorial and a video course as well.

01:59 And then there’s also a bunch of resources about pandas. And here I wanted to highlight the article on the pandas DataFrame that also exists both as a tutorial and the course.

02:09 So these are three directions that you can continue your learning onwards from here. And I hope you had fun and thanks for watching. My name is Martin and hope to see you around here at Real Python.

Become a Member to join the conversation.