Skip to content

Instantly share code, notes, and snippets.

@eseiver
Created August 22, 2017 20:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eseiver/39cdda697119f3cabf7cce181666066c to your computer and use it in GitHub Desktop.
Save eseiver/39cdda697119f3cabf7cce181666066c to your computer and use it in GitHub Desktop.
Check the difference between two strings
# With thanks to https://stackoverflow.com/questions/17904097/python-difference-between-two-strings
# Describes difflib-detected differences in easy-to-understand English
import difflib
cases = [('apple', 'apples'), ('banaana', 'bananas')]
for a, b in cases:
print('{} => {}'.format(a, b))
for i, s in enumerate(difflib.ndiff(a, b)):
if s[0] == ' ': continue
elif s[0] == '-':
print(u'Delete "{}" from position {}'.format(s[-1],i))
elif s[0] == '+':
print(u'Add "{}" to position {}'.format(s[-1],i))
print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment