While refactoring my Python code, I thought of the following question.
Can You Use a Regular Expression with the Python endswith() Method?
The simple answer is no because if you can use a regex, you won’t even need endswith()
! Instead, use the re.match(regex, string)
function from the re
module. For example, re.match("^.*(coffee|cafe)$", tweet)
checks whether a single-line string stored in variable tweet
ends with either 'coffee'
or 'cafe'
.
In fact, I realized that using a regex with the endswith()
method doesnât make sense. Why? If you want to use regular expressions, use functions from the re
module. Thatâs what they were created for! Regular expressions are infinitely more powerful than the endswith()
method!
(Reading time 6 minutes â or watch the video to learn about the string.endswith()
method)
Do you want to master the regex superpower? Check out my new book The Smartest Way to Learn Regular Expressions in Python with the innovative 3-step approach for active learning: (1) study a book chapter, (2) solve a code puzzle, and (3) watch an educational chapter video.
How Does the Python endswith() Method Work?
Hereâs an overview of the string.endswith
method:
str.endswith(prefix[, start[, end]])
prefix | required | String value to be searched at the beginning of string str . |
start | optional | Index of the first position where prefix is to be checked. Default: start=0 . |
end | optional | Index of the last position where prefix is to be checked. Default: end=len(str)-1 . |
Letâs look at some examples using the Python endswith
method. In each one, I will modify the code to show different use cases. Letâs start with the most basic scenario.
Python endswith() Most Basic Example
Suppose you have a list of strings where each string is a tweet.
tweets = ["to thine own self be true", "coffee break python", "i like coffee"]
Letâs say you work in the coffee industry and you want to get all tweets that end with the string "coffee"
. Youâll use the endswith
method with a single argument:
>>> for tweet in tweets: ... if tweet.endswith("coffee"): ... print(tweet) i like 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?
Python endswith() Optional Arguments
The endswith
method has two optional arguments: start
and end
. You can use these to define a range of indices to check. Per default, endswith
checks the entire string. Letâs look at some examples.
The start argument tells endswith()
where to begin searching. The default value is 0, i.e., it begins at the start of the string. So, the following code outputs the same result as above:
>>> for tweet in tweets: ... if tweet.endswith("coffee", 0): ... print(tweet) i like coffee
What happens if we set start=8
?
>>> for tweet in tweets: ... if tweet.endswith("coffee", 8): ... print(tweet)
Why doesnât it print anything? By calling the find()
method, we see that the substring 'coffee'
begins at index 7.
>>> 'i like coffee'.find('coffee') 7
But tweet.endsswith("coffee", 8)
starts looking from index 8. So the result is False
and nothing is printed.
Letâs add another argument â the end
index â to the last snippet:
>>> for tweet in tweets: ... if tweet.startswith("coffee", 7, 9): ... print(tweet) ï»ż
Nothing is printed on the console. This is because we are only searching over two characters – beginning at index 7 (inclusive) and ending at index 9 (exclusive). But we are searching for 'coffee'
and it is 6 characters long. As 6 > 2, endswith()
doesnât find any matches and so returns nothing.
Now that you know everything about Pythonâs endswith
method, letâs go back to our original question:
Can I Use A Regular Expression with the Python endswith() Method?
No. The endswith()
method does not allow for a regular expressions. You can only search for a string.
A regular expression can describe an infinite set of matching strings. For example, '*A'
matches all words ending with 'A'
. This can be computationally expensive. So, for performance reasons, it makes sense that endswith()
doesnât accept regular expressions.
Related article: Python Regex Superpower – The Ultimate Guide
But is it also true that endswith
only accepts a single string as argument? Not at all. It is possible to do the following:
Python endswith() Tuple – Check For Multiple Strings
>>> for tweet in tweets: ... if tweet.endswith(("coffee", "python")): ... print(tweet) coffee break python i like coffee
This snippet prints all strings that end with either "coffee"
or "python"
. It is pretty efficient too. Unfortunately, you can only check a finite set of arguments. If you need to check an infinite set, you cannot use this method.
What Happens If I Pass A Regular Expression To endswith()?
Letâs check whether a tweet ends with any version of the "coffee"
string. In other words, we want to apply the regex ".+coff*"
. This greedily matches any character one or more times, then 'coff'
plus an arbitrary number of characters. Thus we match strings that end with "coffee"
, "coffees"
and "coffe"
.
>>> tweets = ["to thine own self be true", "coffee break python", "i like coffee", "i love coffe", "what's better than one coffee? two coffees!"] >>> for tweet in tweets: if tweet.endswith(".+coff*"): print(tweet) # No output :(
This doesnât work. In regular expressions, *
is a wildcard and represents any character. But in the endswith()
method, it just means the star character *
. Since none of the tweets end with the literal string "coff*"
, Python prints nothing to the screen.
So you might ask:
What Are The Alternatives to Using Regular Expressions in endswith()?
There is one alternative that is simple and clean: use the re
module. This is Pythonâs built-in module built to work with regular expressions.
>>> import re >>> tweets = ["to thine own self be true", "coffee break python", "i like coffee", "i love coffe", "what's better than one coffee? two coffees!"] # Success! >>> for tweet in tweets: if re.match(".+coff*", tweet): print(tweet) i like coffee i love coffe whatâs better than one coffee? two coffees!
Success! Weâve now printed all the tweets we expected. That is, all tweets that end with "coff"
plus an arbitrary number of characters.
Note that this method is quite slow. Evaluating regular expressions is an expensive operation. But the clarity of the code has improved and we got the result we wanted. Slow and successful is better than fast and unsuccessful.
The function re.match()
takes two arguments. First, the regular expression to be matched. Second, the string you want to search. If a matching substring is found, it returns True
. If not, it returns False
. In this case, it returns False
for "to thine own self be true"
and "coffee break python"
. It returns True
for the rest.
So letâs summarize the article.
Can You Use a Regular Expression with the Python endswith() Method?
No, you cannot use a regular expression with the Python endswith
function. But you can use the Python regular expression module re
instead. Itâs as simple as calling the function match(s1, s2)
. This finds the regular expression s1
in the string s2
.
Python endswith() List
Given that we can pass a tuple to endswith()
, what happens if we pass a list?
>>> s = 'cobra' >>> if s.endswith(['a', 'b', 'c']): print('yay!') Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: endswith first arg must be str or a tuple of str, not list
Python raises a TypeError
. We can only pass a tuple to endswith()
. So if we have a list of prefixes we want to check, we can call tuple()
before passing it to endswith
.
>>> if s.endswith(tuple(['a', 'b', 'c'])): print('yay!') yay!
This works well and is fine performance wise. Yet, one of Python’s key features is its flexibility. So is it possible to get the same outcome without changing our list of letters to a tuple? Of course it is!
We have two options:
The any()
function is a way to combine logical or statements together. It takes one argument – an iterable of conditional statements. So instead of writing
if s.endswith('a') or s.endswith('b') or s.endswith('c'): # some code
We write
# any takes 1 argument - an iterable if any([s.endswith('a'), s.endswith('b'), s.endswith('c')]): # some code
This is much nicer to read and is especially useful if you are using many mathematical statements. We can improve this by first creating a list of conditions and passing this to any()
.
letters = ['a', 'b', 'c'] conditions = [s.endswith(l) for l in letters] if any(conditions): # do something
Alternatively, we can use map
instead of a list comprehension
letters = ['a', 'b', 'c'] if any(map(s.endswith, letters)): # do something
Both have the same outcome. We personally prefer list comprehensions and think they are more readable. But choose whichever you prefer. Â
Regex Humor

Python Regex Course
Google engineers are regular expression masters. The Google search engine is a massive text-processing engine that extracts value from trillions of webpages. Â
Facebook engineers are regular expression masters. Social networks like Facebook, WhatsApp, and Instagram connect humans via text messages.Â
Amazon engineers are regular expression masters. Ecommerce giants ship products based on textual product descriptions.  Regular expressions ârule the game âwhen text processing âmeets computer science.Â
If you want to become a regular expression master too, check out the most comprehensive Python regex course on the planet:

While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.