I am sitting in front of my computer refactoring my Python code and I am asking myself the following question. So I did a bit of research that I want to share with you.
Is it possible to use a regular expression within the Python endswith() method?
The short answer is no. In fact, I realized that using a regex does not make any sense in combination with the endswith() method. Instead, you should use import the re module and use re.search(regex, string).
How does the Python endswith(suffix[,start[,end]]) method work?
This article leads you step-by-step through the Python endswith method. In each step, I will slightly modify the original code snippet to showcase different uses. Let’s start with the basic scenario: Suppose you have a set of tweets in form of strings.
d =  d.append("to thine own self be true") d.append("coffee break python") d.append("i like coffee")
Say, you work in the coffee industry. You want to filter out all tweets that end with the string ‘coffee’. Basically, you check the string on which you call the method whether it has the specified suffix. In its most basic form, the endswith method takes only a single argument: the suffix. Here is an example:
for tweet in tweets: if tweet.endswith("coffee"): print(tweet)
When executing this snippet, the resulting output on your console is “i like coffee”. It’s the only tweet from our toy database that ends with the string “coffee”.
The endswith method has two optional arguments: start and end. You can use these two arguments to check whether a substring from the original string ends with your argument. Need an example that explains both arguments start and end?
The start argument sets the start index of the matching suffix. Per default, it is set to 0. Hence, the following code snippet produces exactly the same result as the last code snippet:
for tweet in tweets: if tweet.endswith("coffee", 0): print(tweet)
However, what do you think happens when we set the start argument to the value 8? Have a look at the following code snippet:
for tweet in tweets: if tweet.endswith("coffee", 8): print(tweet)
When executing this snippet, you realize that nothing is printed to the standard output. The reason is that the string “coffee” is not a suffix of any tweet, when starting from index 8. Hence, when checking tweet.endswith(“coffee”, 8) for the tweet ‘i like coffee’, the result is False.
So let’s add another argument – the end index – to the last snippet:
for tweet in tweets: if tweet.endswith("coffee", 6, 9): print(tweet)
Nothing is printed to the console. The reason is the short substring carved out from the original tweets. This substring is only three characters long – beginning from index 6 (inclusive) and ending in index 9 (exclusive). But the searched string “coffee” is six characters long. It’s impossible that “coffee” is the suffix of a three-character long string.
Now you know everything you need to know about Python’s endswith method. So coming back to your question:
Can I use a regular expression with Python endswith()?
No. The endswith method does not allow for a regular expression. You can only search for a string suffix. This makes sense. A regular expression can describe an infinite set of matching strings. For example, the regex ‘*A’ matches all words ending with the character ‘A’. Clearly, this operation can be computationally expensive.
But is it also true that the endswith method allows only a single string as an argument? Not at all. It is possible to do the following:
How to check endswith for multiple strings (tuple argument)?
tweets =  tweets.append("to thine own self be true") tweets.append("coffee break python") tweets.append("i like coffee") for tweet in tweets: if tweet.endswith(("coffee", "i")): print(tweet)
This snippet prints all tweets that end with either “coffee” or “i”. The result is the tweet ‘i like coffee’. It is pretty efficient to do this. Unfortunately, it allows checking for only a finite set of arguments whether they are a suffix of the original string. If you need to check a more powerful expression, you can not use this way.
To be frank, it would not make too much sense to use a regular expression within the endswith method. Why? Suppose you are ok with using a regular expression. Now you could simply use this regex to achieve the exact same thing you want to do with the endswith method. But without the unnecessary wrapper function endswith().
In the next example, let’s check for the regular expression “c….?e$”. This regex searches for occurrences of a substring starting with the character c, followed by three or four arbitrary characters, followed by the character e at the end of the string.
You searched for something like this, right?
tweets =  tweets.append("to thine own self be true") tweets.append("coffee break python") tweets.append("i like coffee") tweets.append("i love coffe") # WRONG!! for tweet in tweets: if tweet.endswith("c....?e$"): print(tweet)
This is not working. The regex is interpreted as simple string. Of course, the string “c….?e$” does not occur in any tweet. You can not use a regex within the endswith() method.
So you might ask:
What are alternatives to using a regular expression in endswith()?
There is one alternative that is incredibly simple and clean. Use the re package. Here is an example code snippet that demonstrates the use of the re package.
import re tweets =  tweets.append("to thine own self be true") tweets.append("coffee break python") tweets.append("i like coffee") tweets.append("i love coffe") # CORRECT!! for tweet in tweets: if re.search("c....?e$", tweet): print(tweet)
This code snippets prints both strings “i like coffee” and “i love coffe”. Both matching the regex. Although this method may be a bit slow (evaluating regular expressions is an expensive operation), the clarity of the code base has improved a lot. Note that the regular expression operator re.search(…) requires you to define two arguments. First, you have to define the regular expression to be matched. Second, you have to define the string in which you want to look for the regex. If a matching substring is found, it returns a matching object to the user (that evaluates to True if used within the if statement).
So let’s summarize the article.
Can I use a regular expression in the Python endswith method?
No, you cannot use a regular expression within the Python endswith method. But you can use the Python regular expression module re to do the job for you. It’s as simple as calling the function search(s1, s2) that attempts to find the regular expression s1 in the string s2.
Where to go from here?
Do you struggle understanding Python code? I have written a book with the title “Coffee Break Python: 50 Workouts to Kickstart Your Rapid Code Understanding in Python”. The book is for you if you want to become a Python pro but have little time to learn Python. A short daily coffee break Python is enough to boost your skills! Check out the book!
Register for our free email course to grow your Python skills on autopilot. Why should you do that? Because I am sending you an educative Python email every few days. You will be challenged. You will grow your skills. You will become a better coder.