---
title: "Discussion on \"How to Compare Two Strings in Python (in 8 Easy Ways)\" | Hashnode"
description: "Discussion on \"How to Compare Two Strings in Python (in 8 Easy Ways)\". A guide on how to check if two strings are equal, or similar. Learn how to find the difference between two strings. Make complex comparisons and more!"
url: "https://hashnode.com/posts/python-compare-strings/61a35c66d51d1f4e7e1c6e75"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [How to Compare Two Strings in Python (in 8 Easy Ways)](https://miguendes.me/python-compare-strings?utm_source=hashnode&utm_medium=feed)

A guide on how to check if two strings are equal, or similar. Learn how to find the difference between two strings. Make complex comparisons and more!

Nov 28, 2021 · 12 min read · miguendes.me  
Updated Jul 2, 2022  
Read the article

-   [#python](https://hashnode.com/tag/python)
-   [#python3](https://hashnode.com/tag/python3)
-   [#python-beginner](https://hashnode.com/tag/python-beginner)

## Written by

[Miguel Brito](https://hashnode.com/@miguendes)

@miguendes · London, UK

  
[View profile](https://hashnode.com/@miguendes)

Machine Learning Software Engineer - Hobbyst Technical Writer - Interested in Software Testing, Best Practices, Scalability, and Python.

AI Software Engineer based in London, UK with 6+ years of professional experience developing and releasing software in different programming languages - Hobbyist Technical Writer - Interested in Software Testing, Best Practices, Scalability, Machine Learning/AI, and Python.

## Responses 4

Markdown supported · ⌘↵ to post

[Gyansetu](https://hashnode.com/@gyansetu)

[Oct 17, 2023](https://hashnode.com/posts/python-compare-strings/61a35c66d51d1f4e7e1c6e75/comment/652e866cd61db22825b6d92a)

Python is the language of the future, and Gyansetu's Python courses are the perfect gateway to this exciting world. Their expert instructors and practical approach make learning Python a breeze. Whether you're a beginner or looking to level up your skills, Gyansetu has you covered! For more info:- [gyansetu.in/blogs/future-scope-of-python-in-india](https://www.gyansetu.in/blogs/future-scope-of-python-in-india/)

[Oluwaseun Da Silva Aladejana](https://hashnode.com/@devsilva)

[Sep 21, 2022](https://hashnode.com/posts/python-compare-strings/61a35c66d51d1f4e7e1c6e75/comment/632aee82195e7dc741da9f27)

This is insightful

[Raghu](https://hashnode.com/@raghu786)

[Jun 25, 2022](https://hashnode.com/posts/python-compare-strings/61a35c66d51d1f4e7e1c6e75/comment/62b722bc92447c9aed5b7ec3)

Hi, would need your thoughts to find the diff between the two strings. Have gone through the article but somehow I could not fetch the write solutions.

c1 = 'X+v//wwAAAAmNjY0Wm+DhsDZ2TyBnLw9JDFw2YUsthQViGonKRuO/UZETzIzMTYx'

c2 = 'X+v//wwAAAAmNjY0Wm+DhsDZ2TyBnLw9JDFw2YUsthQViGonKRuO/UZETzIzMTYx'

c1 is c2 returns False but c1 == c2 returns True Not sure how to fix this error.

[Frédéric La Rosa](https://hashnode.com/@ElMeTeOr)

[Dec 7, 2021](https://hashnode.com/posts/python-compare-strings/61a35c66d51d1f4e7e1c6e75/comment/61af53bf079fa33c0860f19d)

Hi thank you for all!

I'm begin to Python and i'm trying to make a function, with a condition that finds a row of characters following, between two list of words.

I search something like this:

```
List1 = ['Birmanie', 'Biélorussie', 'Bolivie', 'Brunei', 'Burkina', 'Bélize', 'Cap-Vert']

List2 = ['Belize', 'Bermudes', 'Bolivie (État plurinational de)', 'Brunéi Darussalam', 'Burkina Faso', 'Bélarus', 'Cabo Verde']

def matchingword(number_of_character_following = 3)
    for number_of_characters_in_a_row in list1:
        if number_of_characters_in_a_row in list2:
            return possible_matching_words in []
        else:
            return no_matching_words in []
```

My result should be:

possible\_matching\_words = \['Biélo**rus**sie':'Béla**rus**', 'Biélo**rus**sie':'Brunéi Da**rus**salam', '**Bol**ivie':'**Bol**ivie (État plurinational de)', '**Bru**nei':'**Bru**néi Darussalam', '**Bur**kina':'**Bur**kina Faso', 'Cap-**Ver**t':'Cabo **Ver**de'\]

How can i do please?

[Miguel Brito](https://hashnode.com/@miguendes)

[Dec 7, 2021](https://hashnode.com/posts/python-compare-strings/61a35c66d51d1f4e7e1c6e75/comment/61af53bf079fa33c0860f19d#reply-61afb4be9759e33b4eaf94fa)

Hey [Frédéric La Rosa](https://hashnode.com/@ElMeTeOr), you can use the `difflib` for that.

Example:

```
In [1]: List1 = ['Birmanie', 'Biélorussie', 'Bolivie', 'Brunei', 'Burkina', 'Bélize', 'Cap-Vert']


In [2]: List2 = ['Belize', 'Bermudes', 'Bolivie (État plurinational de)', 'Brunéi Darussalam', 'Burkina Faso', 'Bélarus', 'Cabo Verde']


In [5]: from difflib import SequenceMatcher

In [8]: def find_longest_matching(list1, list2, match_len=3):
   ...:     res = []
   ...:     for s1 in list1:
   ...:         for s2 in list2:
   ...:             match = SequenceMatcher(None, s1, s2).find_longest_match(0, len(s1), 0, len(s2))
   ...:             if match.size >= match_len:
   ...:                 res.append({'s1': s1, 's2': s2, 'match': s1[match.a: match.a + match.size]})
   ...:     return res
   ...: 

In [9]: find_longest_matching(List1, List2)
Out[9]: 
[{'s1': 'Biélorussie', 's2': 'Brunéi Darussalam', 'match': 'russ'},
 {'s1': 'Biélorussie', 's2': 'Bélarus', 'match': 'rus'},
 {'s1': 'Bolivie',
  's2': 'Bolivie (État plurinational de)',
  'match': 'Bolivie'},
 {'s1': 'Brunei', 's2': 'Brunéi Darussalam', 'match': 'Brun'},
 {'s1': 'Burkina', 's2': 'Bolivie (État plurinational de)', 'match': 'ina'},
 {'s1': 'Burkina', 's2': 'Burkina Faso', 'match': 'Burkina'},
 {'s1': 'Bélize', 's2': 'Belize', 'match': 'lize'},
 {'s1': 'Bélize', 's2': 'Bélarus', 'match': 'Bél'},
 {'s1': 'Cap-Vert', 's2': 'Cabo Verde', 'match': 'Ver'}]
```

Is that what you want? You tweak this function to return only the exact match length you look for, like 3.

[Frédéric La Rosa](https://hashnode.com/@ElMeTeOr)

[Dec 8, 2021](https://hashnode.com/posts/python-compare-strings/61a35c66d51d1f4e7e1c6e75/comment/61af53bf079fa33c0860f19d#reply-61b00ea19759e33b4eaf99e7)

[Miguel Brito](https://hashnode.com/@miguendes) Great Miguel, thank you! I find with a friend this function, using the levenshtein distance:

```
ListEtats = ['Birmanie', 'Biélorussie', 'Bolivie', 'Brunei', 'Burkina', 'Bélize', 'Cap-Vert', 'Centrafrique', 'Chine', 'Corée du Nord', 'Corée du Sud', 'Guatémala', 'Guinée Bissao', 'Irak', 'Iran', 'Kirghizstan', 'Kosovo', 'Kénya', 'Laos', 'Liechtenstein', 'Lésotho', 'Micronésie', 'Moldavie', 'Monaco', 'Niue', 'Royaume-Uni', 'Russie', 'Saint-Christophe et Niévès', 'Saint Marin', 'Saint Vincent et les Grenadines', 'Salomon', 'Salvador', 'Syrie', 'Tanzanie', 'Timor oriental', 'Vatican', 'Vietnam', 'Vénézuéla', 'Zimbabwé', 'États Unis']

ListZones = ['Belize', 'Bermudes', 'Bolivie État plurinational de', 'Brunéi Darussalam', 'Burkina Faso', 'Bélarus', 'Cabo Verde', 'Chine RAS de Hong Kong', 'Chine RAS de Macao', 'Chine Taiwan Province de', 'Chine continentale', 'El Salvador', 'Fédération de Russie', 'Groenland', 'Guatemala', 'Guinée Bissau', 'Iran République islamique d', 'Iraq', 'Kenya', 'Kirghizistan', 'Lesotho', 'Micronésie États fédérés de', 'Myanmar', 'Nioué', 'Nouvelle Calédonie', 'Palestine', 'Polynésie française', 'Porto Rico',  'Royaume Uni de Grande-Bretagne et d Irlande du Nord', 'République arabe syrienne', 'République centrafricaine', 'République de Corée', 'République de Moldova', 'République démocratique populaire lao', 'République populaire démocratique de Corée', 'République Unie de Tanzanie', 'Saint Kitts et Nevis', 'Saint Vincent et les Grenadines', 'Samoa américaines', 'Timor Leste', 'Tokélaou', 'Venezuela République bolivarienne du', 'Viet Nam', 'Zimbabwe', 'États Unis d Amérique', 'Îles Salomon']

import jellyfish

def compareList(list1, list2):  
    dict = {}
    for item1 in list1:
        for item2 in list2:
            current_leven = jellyfish.jaro_distance(item1, item2)
            if item1 not in dict or current_leven > jellyfish.jaro_distance(item1, dict[item1][0]):
                dict[item1] = (item2, current_leven)
    return dict

compareList(ListEtats, ListZones)
```

This is good but i think i need to optimise this one by applying your method in a second argument.

Thank you again for your help!
