In this tutorial, we are going to find a solution to a problem. Let's see what the problem is. We have a list of strings and an element. We have to find strings from a list in which they must closely match to the given element. See the example.
Inputs strings = ["Lion", "Li", "Tiger", "Tig"] element = "Lion" Ouput Lion Li
We can achieve this by using the startswith built-in method. See the steps to find the strings.
Print the string
## initializing the string list strings = ["Lion", "Li", "Tiger", "Tig"] element = "Lion" for string in strings: ## checking for the condition mentioned above if string.startswith(element) or element.startswith(string): ## printing the eligible string print(string, end = " ") print()
If you run the above program,
If you have any doubts regarding the program, please do mention them in the comment section.