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.
