Sunday, November 17, 2024
Google search engine
HomeLanguagesPython – Check if string starts with any element in list

Python – Check if string starts with any element in list

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




# initializing string
test_string = "GfG is best"
 
# initializing prefix list
pref_list = ['best', 'GfG', 'good']
 
# printing original string
print("The original string : "
    + str(test_string))
 
# using startswith()
# Prefix tests in Strings
res = test_string.startswith(tuple(pref_list))
 
# print result
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




# initializing string
test_string = "GfG is best"
 
# initializing prefix list
pref_list = ['best', 'GfG', 'good']
 
pref_list1 = ['GeeksforLazyroar']
 
# printing original string
print("The original string : " + str(test_string))
 
# test True case
res = list(filter(test_string.startswith, pref_list)) != []
 
# print result
print("Does string start with any prefix list sublist ? : " + str(res))
 
# test False case
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




# initializing string
test_string = "GfG is best"
 
# initializing prefix list
pref_list = ["cat", "dog"]
 
# printing original string
print("The original string : " + str(test_string))
 
res=False
x=test_string.split()
if(x[0] in pref_list):
    res=True
 
# print result
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




# Python3 code to demonstrate
# Prefix tests in Strings
 
# initializing string
test_string = "GfG is best"
 
# initializing prefix list
pref_list = ['best', 'GfG', 'good']
 
# printing original string
print("The original string : " + str(test_string))
 
res=False
for i in pref_list:
    if(test_string.find(i)==0):
        res=True
# print result
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




# Python3 code to demonstrate
# Prefix tests in Strings
import operator as op
# initializing string
test_string = "GfG is best"
 
# initializing prefix list
pref_list = ['best', 'GfG', 'good']
 
# printing original string
print("The original string : " + str(test_string))
 
res = False
for i in pref_list:
    if(op.countOf(test_string, i) == 0):
        res = True
# print result
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
 
# initializing string
test_string = "GfG is best"
 
# initializing prefix
pref = 'best'
 
# printing original string
print("The original string : " + str(test_string))
 
# using regular expressions
res = re.match("^" + pref, test_string)
 
# print result
print("Does string start with prefix ? : " + str(bool(res)))
#This code is contributed by Vinay Pinjala.


Output

The original string : GfG is best
Does string start with prefix ? : False

Time Complexity: O(n)
Auxiliary Space: O(1)

RELATED ARTICLES

Most Popular

Recent Comments