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.
- Initialize string list and a string.
- Loop through the list.
- If string from list startswith element or element startswith the string from the list
Print the string
Example
## 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,
Output
Lion Li
If you have any doubts regarding the program, please do mention them in the comment section.