You are currently viewing How to Calculate String Similarity Score in Python

How to Calculate String Similarity Score in Python

In Python, the difflib library contains functions for computing and working with differences between sequences. One of these functions is the SequenceMatcher class, which can be used to compute the string similarity score of two strings. This function will take two strings and return the similarity score.

Example 1:

import difflib

str_1 = "This is the string one example."
str_2 = "This is the string two example."

similarity_score = difflib.SequenceMatcher(None, str_1, str_2).ratio()
print(similarity_score)

Output:

0.9354838709677419

Example 2:

import difflib

str_1 = "AiOcta"
str_2 = "aiocta"

similarity_score = difflib.SequenceMatcher(None, str_1, str_2).ratio()
print(similarity_score)

Output:

0.6666666666666666

Note: The difflib.SequenceMatcher() is case sensitive. In example 2, you can see that the library is considering “AiOcta” and “aiocta” two different strings and returning the similarity score 0.6666666666666666 instead of 1.0.

References:

https://docs.python.org/3/

Leave a Reply