Problem Formulation: How to Test if String Starts With Any Prefix From an Iterable?
Given a string and multiple string prefixes in an iterable such as a list or a tuple.
How to test whether the string starts with any of the provided prefixes?
Here are three examples:
s = 'hello world' prefixes = ['hi', 'bye', 'no', 'hello'] # True s = 'finxter' prefixes = ['p', 'f', 'fi', 'xt'] # True s = 'mimi' prefixes = ['a', 'b', 'c'] # False
Overview Solutions
There are three ways to test if a string starts with any prefix from an iterable of strings:
s.startswith(tuple(prefixes))
any(s.startswith(x) for x in prefixes)
any(map(s.startswith, prefixes))
Let’s dive into each of those next!
Method 1: Pass Tuple of Prefixes
To check if a given string starts with any of multiple prefixes
, convert the iterable of prefixes into a tuple and pass it into the string.startswith()
method like so: s.startswith(tuple(prefixes))
.
The following code checks if the string 'hello world'
begins with any of a number of prefixes.
s = 'hello world' prefixes = ['hi', 'bye', 'no', 'hello'] result = s.startswith(tuple(prefixes)) print(result) # True
In case you wonder: here’s a more formal overview of the string.startswith()
method:
str.startswith(prefix[, start[, end]])
prefix | required | String value to be searched at the beginning of string str . Can also be a tuple of prefixes. |
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 . |
But there’s another elegant solution based on the any()
function. Let’s examine this next!
Method 2: Use the any() Function
To check if a given string starts with any of multiple prefixes
, you can use the any(iterable)
function that returns True
if at least one of the values in the iterable evaluates to True
. You can check each prefix against the string by using a generator expression like so: any(s.startswith(x) for x in prefixes)
.
Here’s the same example as before:
s = 'hello world' prefixes = ['hi', 'bye', 'no', 'hello'] result = any(s.startswith(x) for x in prefixes) print(result) # True
The generator expression s.startswith(x) for x in prefixes
generates Boolean values until it finds a True
value. In this case, it short-circuits the execution and the any()
function immediately returns True
—a prefix has been found.
You can also use the map()
function instead of the generator expression to transform each prefix into a Boolean value:
Method 3: Use the map() Function
The map()
function transforms an iterable into a new one by applying a βtransformator functionβ to each element. You can transform each possible prefix string into a Boolean value whether this prefix is indeed a prefix of a given string. The resulting iterable of Boolean values can then be checked for a True
value using the any()
function like so: any(map(s.startswith, prefixes))
.
Here’s the same example as before using this method:
s = 'hello world' prefixes = ['hi', 'bye', 'no', 'hello'] result = any(map(s.startswith, prefixes)) print(result) # True
Summary
There are three ways to test if a string starts with any prefix from an iterable of strings:
s.startswith(tuple(prefixes))
any(s.startswith(x) for x in prefixes)
any(map(s.startswith, prefixes))
If you want to learn about each of these techniques and become a programming master, check out the Finxter Premium Membership, an inexpensive subscription for computer science and Python courses that will boost your code understanding to levels never seen before!