8) Text and Strings Lesson

Python Substring

6 min to complete · By Martin Breuss

In this section, you've learned a lot about strings in Python! It's time to take a break and have some fun. Of course, while also learning something new :) In this lesson, you will get to know how to find substrings in a string.

Then, you'll get a chance to apply some of what you've learned in your Python course labs. The labs are your training exercises to get in the necessary practice, as well as to challenge you to think through concepts for yourself, which helps your neurons to fire and build new connections. Time to get started!

What is a Substring

Sometimes, you'll want to find out whether there's a certain word or substring inside of a given text. Python offers you a couple of ways to do this. Here, you'll look at string methods that allow you to search in a user-friendly manner.

Python .endswith Method

Think back to an example where you have a bunch of filenames and you want to filter out only some of them with just the right file extension. For that, you can use the str.endswith() method, which you might have already found in the string method documentation:

"mytext.txt".endswith("txt")  # True
"mypage.php".endswith("txt")  # False

This string method will check whether the argument you passed to the method, in this case, that's the string "txt", is indeed how the string you're calling the method on ends. In the example above, you are calling the method on both "mytext.txt" and "mypage.html", one of which does end with "txt", and the other one doesn't.

How Does it Work

Python returns a truth value as a response to your question. You can think of what's going on up there like this:

You are asking Python: The string "mytext.txt" ends with the substring "txt", right? And because it does, Python responds by saying: Yep, that's True.

Great! Python is a good buddy and responds truthfully. Or is it just trying to please you? You should check:

So, next you ask Python: The string "mypage.php" ends with the substring "txt", right? And because it does not, Python keeps it real and responds with saying: Nope, that's False.

As you can see, str.endswith() is a convenient and user-friendly method that almost reads like English. And it's by far not the only string method that allows you to find substrings in text:

Python String Methods

Check out some other interesting string methods that can help you find substrings in text in the string method documentation.

  • Find the method to check whether a string starts with a certain substring.
  • Find a method that allows you to determine at what index position a certain substring exists.

After you finish these tasks, you should know about at least three methods that allow you to find substrings within the text.

However, sometimes you don't need to know anything about the start or the end of a string, nor do you care about where a substring exists. Instead, you just want to know whether a substring is anywhere in a larger text.

There's a user-friendly way to do that, too! You'll learn about how to do that in the upcoming section that will introduce you to Python's operators.

Colorful illustration of a light bulb

Additional Resources

Summary: Python Substring

  • There are multiple ways to find substrings within strings in Python
  • str.endswith() method checks whether or not a string ends with a specific substring
  • The string method documentation is a good resource to learn about additional substring methods