Ad

Compare Two Strings And Assign Score In Python

- 1 answer

I need to know whether we can compare 2 string of different length and check the substring is occured in given sring or not

For example:

str1='abcdef '
str2='acbcdef'

here length are same but letter is different how can i assign score for this

Ad

Answer

Use difflib:

from difflib import SequenceMatcher

def similar(a, b):
    return SequenceMatcher(None, a, b).ratio()
str1='abcdef '
str2='acbcdef'
print(similar(str1,str2))

Output:

0.8571428571428571
Ad
source: stackoverflow.com
Ad