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

Unlock This Lesson

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

Unlock This Lesson

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 see our video player troubleshooting guide to resolve the issue.

Learn More Using String Methods

00:00 In this part of the course, you’ll learn more about a couple of Python string methods that you can use to learn more about the substring. I’ll start off by showing you a little table that gives you an overview about what’s the name of the string method, what happens if it does find a substring, and what happens if it doesn’t find the substring. One that you’re going to look at is str.count(), and this is a way to get a count of how often does a substring appear in a string. So if it finds the substring, it gives you back an integer that tells you how often it is found inside of the string. And if it’s never found, then it returns the integer 0.

00:39 Then there’s str.index(). That’s a way for you to locate the substring in the string. And what it does, it gives you back the start index of where the first occurrence of the substring is found inside of the string that you’re searching in.

00:53 What it does when there is no substring in the string is to raise a ValueError.

00:59 And then there’s also str.find() that does the same thing as str.index() in the success condition. So if it finds the substring, then it gives you back the starting index position.

01:10 But if the substring isn’t found, then instead of raising a ValueError, it returns -1. I’ll tell you more about str.index() and str.find() throughout this course.

01:20 You may have seen str.find() used to check whether a substring is in the string. I don’t consider this a good way to do it, but I’ll show you how to do it later on in the course so that you also can maybe see for yourself how it works and why using the in operator is a better choice.

01:37 But all of these string methods have their use cases. They are useful for learning more about the substring. And now let’s head back over to the REPL to try them out and learn a little more about your substring.

Become a Member to join the conversation.