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:
- Strings and Character Data in Python
- Regular Expressions and Building Regexes in Python
- The pandas DataFrame: Working With Data Efficiently
You might also enjoy these written tutorials:
- Strings and Character Data in Python
- Regular Expressions: Regexes in Python (Part 1)
- Regular Expressions: Regexes in Python (Part 2)
- The pandas DataFrame: Make Working With Data Delightful
Congratulations, you made it to the end of the course! What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment in the discussion section and let us know.