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. For data manipulation tasks, we may need to sometimes, check if a string starts with any of the matching strings. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using filter() + startswith()
The combination of the above function can help to perform this particular task. The filter method is used to check for each word and startswith method tests for the prefix logic at target list.
# Python3 code to demonstrate # Prefix tests in Strings # using filter() + startswith() # 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 filter() + startswith() # Prefix tests in Strings res = list(filter(test_string.startswith, pref_list)) != [] # print result print("Does string start with any prefix list sublist ? : " + str(res)) |
The original string : GfG is best Does string start with any prefix list sublist ? : True
Method #2 : Using startswith()
As improvement to the above method, it is not always necessary to include filter method for comparison. This task can be handled solely by supplying a prefix check list as argument to startswith method as well.
# Python3 code to demonstrate # Prefix tests in Strings # using startswith() # 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)) |
The original string : GfG is best Does string start with any prefix list sublist ? : True
Recommended Posts:
- Python - Check whether a string starts and ends with the same character or not (using Regular Expression)
- Python | Check if given string can be formed by concatenating string elements of list
- Python | Check if any element in list satisfies a condition
- Python | Ways to check if element exists in list
- Python | Check if any element occurs n times in given list
- Python | Check if element exists in list of lists
- Python | Check if string ends with any string in given list
- Python | Test if string contains element from list
- Python | Convert String list to Joined Single element
- Python | Check if any String is empty in list
- Python | Check if string matches regex list
- Python | Check if suffix matches with any string in given list
- Python | Replace elements in second list with index of same element in first list
- Python | Subtract two list elements if element in first list is greater
- Python | Indices list of matching element from other list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

