Python – Check if string starts with any element in list
Last Updated :
20 Apr, 2023
While working with strings, their prefixes and suffix play an important role in making any decision. Let’s discuss certain ways in which this task can be performed.
Example:
String = "GfG is best"
Input_lis = ['best', 'GfG', 'good']
Output: True
Explanation: 'GfG is best' is present in the list.
String = "GfG is best"
Input_lis = ['Good', 'Bad', 'Nice']
Output: False
Explanation: 'GfG is best' is not present in the list.
Check if a string starts with any element in a list using string startswith()
This task can be handled solely by using string startswith() supplying a prefix checklist as an argument to a method as well.
Python3
test_string = "GfG is best"
pref_list = [ 'best' , 'GfG' , 'good' ]
print ( "The original string : "
+ str (test_string))
res = test_string.startswith( tuple (pref_list))
print ( "Does string start with any prefix list sublist ? : "
+ str (res))
|
Output
The original string : GfG is best
Does string start with any prefix list sublist ? : True
Check if a string starts with any element in a list using filter()
The filter() method is used to check for each word and startswith() method tests for the prefixing logic at the target list.
Python3
test_string = "GfG is best"
pref_list = [ 'best' , 'GfG' , 'good' ]
pref_list1 = [ 'Geeksforgeeks' ]
print ( "The original string : " + str (test_string))
res = list ( filter (test_string.startswith, pref_list)) ! = []
print ( "Does string start with any prefix list sublist ? : " + str (res))
res = list ( filter (test_string.startswith, pref_list1)) ! = []
print ( "Does string start with any prefix list sublist ? : " + str (res))
|
Output
The original string : GfG is best
Does string start with any prefix list sublist ? : True
Does string start with any prefix list sublist ? : False
Check if a string starts with any element in a list using in operator
In this example, we are using a Python loop to check if the existing string words are contained in a list or not. Here, we first assigned res to false, if the condition gets satisfied then the result becomes True.
Python3
test_string = "GfG is best"
pref_list = [ "cat" , "dog" ]
print ( "The original string : " + str (test_string))
res = False
x = test_string.split()
if (x[ 0 ] in pref_list):
res = True
print ( "Does string start with any prefix list sublist ? : " + str (res))
|
Output
The original string : GfG is best
Does string start with any prefix list sublist ? : False
Time complexity: O(n), where n is the length of the prefix list.
Auxiliary space: O(1) as we are not using any additional data structures.
Using find() method
Python3
test_string = "GfG is best"
pref_list = [ 'best' , 'GfG' , 'good' ]
print ( "The original string : " + str (test_string))
res = False
for i in pref_list:
if (test_string.find(i) = = 0 ):
res = True
print ( "Does string start with any prefix list sublist ? : " + str (res))
|
Output
The original string : GfG is best
Does string start with any prefix list sublist ? : True
Time complexity: O(n)
Auxiliary space: O(1)
Using operator.countOf() method
Python3
import operator as op
test_string = "GfG is best"
pref_list = [ 'best' , 'GfG' , 'good' ]
print ( "The original string : " + str (test_string))
res = False
for i in pref_list:
if (op.countOf(test_string, i) = = 0 ):
res = True
print ( "Does string start with any prefix list sublist ? : " + str (res))
|
Output
The original string : GfG is best
Does string start with any prefix list sublist ? : True
Time Complexity: O(n)
Auxiliary Space: O(1)
Using regular expressions
Python3
import re
test_string = "GfG is best"
pref = 'best'
print ( "The original string : " + str (test_string))
res = re.match( "^" + pref, test_string)
print ( "Does string start with prefix ? : " + str ( bool (res)))
|
Output
The original string : GfG is best
Does string start with prefix ? : False
Time Complexity: O(n)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...