import difflib string1 = "Python" string2 = "Pithon" lev_distance = difflib.SequenceMatcher(None, string1, string2).ratio() print(lev_distance)
import difflib string1 = "Python" string2 = "Java" ham_distance = difflib.ndiff(string1, string2) diff_count = 0 for i in ham_distance: if i[0] != ' ': diff_count += 1 print(diff_count)This code calculates the Hamming distance between two strings by counting the number of differing characters at the same position in the two strings. In conclusion, we have used the `difflib` package in Python to calculate Levenshtein and Hamming distances between two strings. This package offers various built-in functions like SequenceMatcher, ndiff, etc. to compute these text distances.