
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find All Close Matches of Input String from a List in Python
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.
Advertisements