Using difflib in Python for fuzzy string matching

Posted on 10 Aug, 2021

>>> from difflib import get_close_matches                                  
>>> fruits = ["apple", "orange", "banana", "peach"]                        
>>> get_close_matches('app', fruits)                                       
['apple']                                                                  
>>> get_close_matches('aple', fruits)                                      
['apple']                                                                  
>>> get_close_matches('ach', fruits)                                       
['peach']                                                                  
>>> get_close_matches('ba', fruits)                                        
[]                                                                         
>>> get_close_matches('ban', fruits)                                       
['banana']  
  • A nice use case for this in CLI when a user enter a wrong sub-command, we can suggest or automatically run the correct command

Last updated