In this tutorial, we will look at how to check if a string in Python ends with any value in a specified list with the help of some examples.
How to check if a string ends with any item in a list?
You can use a combination of the Python built-in any()
function and the string endswith()
function to check if a string ends with any value in a list.
The built-in any()
function in Python takes an iterable as an argument and returns True
if any value in the iterable is truthy (evaluates to True
in a boolean context).
So, for each value in the list, check if the given string ends with that value or not using the string endswith()
function and pass the resulting boolean iterable as an argument to the any()
function. The following is the syntax –
# check if string ends with any in list any(s.endswith(w) for w in ls)
It returns True
if the string s
ends with a value that is present in the list ls
.
Examples
Let’s now look at some examples. First, we will create some strings that we will use throughout this tutorial.
# create strings s1 = "Cleveland" s2 = "Portland" s3 = "Michigan" s4 = "Indianapolis" s5 = "" # display the strings print(s1) print(s2) print(s3) print(s4) print(s5)
Output:
Cleveland Portland Michigan Indianapolis
Here, we created five strings – s1
, s2
, s3
, s4
, and s5
. The first four strings are city names in the USA and the last string s5 is an empty string.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
Let’s now check if the above strings end with a value that is present in a specified list ls
or not.
We’ll a list comprehension to check if a string ends with a value in the given list or not and pass the resulting boolean list as an argument to the any()
function.
# create a list of accepted ending values ls = ["land", "polis", "ville"] # check if string ends wtih a value in ls print(any([s1.endswith(w) for w in ls])) print(any([s2.endswith(w) for w in ls])) print(any([s3.endswith(w) for w in ls])) print(any([s4.endswith(w) for w in ls])) print(any([s5.endswith(w) for w in ls]))
Output:
True True False True False
We get True
for s1
, s2
and s4
as they end with values that are present in the list ls
. We get False
s3
as it does not end with a value that is present in the list ls
. Note that we get False
for the empty string.
The any()
function takes an iterable as an argument, you can also directly pass an iterator which results in an iterable to the any()
function (and avoid using the list comprehension).
# create a list of accepted ending values ls = ["land", "polis", "ville"] # check if string ends wtih a value in ls print(any(s1.endswith(w) for w in ls)) print(any(s2.endswith(w) for w in ls)) print(any(s3.endswith(w) for w in ls)) print(any(s4.endswith(w) for w in ls)) print(any(s5.endswith(w) for w in ls))
Output:
True True False True False
We get the same result as above.
Now, in case you want to make the above check case-insensitive (returns True
if the string ends with a value in the list irrespective of the case, use the string lower()
function).
# check if string ends wtih a value in ls irrespective of case print(any(s1.lower().endswith(w.lower()) for w in ls))
Output:
True
You might also be interested in –
- Python – Check If String starts with a Number
- Python – Check If String Starts with a Letter
- Python – Check If a Character Appears Twice in String
- Python – Check If String Contains Vowels
- Python – Check If String Contains Lowercase Letters
- Python – Check If String Contains Uppercase Letters
- Python String Uppercase – With Examples
- Python String Lowercase – With Examples
- Python – Capitalize First Letter of Each Word
- Python – Check If All Characters in String are Same
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.