Learn More Using String Methods
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
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.
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.