Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Comparing 2 identical strings returns 'False'

When I compare these 2 strings, the value I get is False.

a = "comentar"
b = "️comentar"
print(a == b) # False

How could I fix this? I have tried changing the encoding of both strings but it does not have any effect.

You can try it here: https://onlinegdb.com/HJ8xYLPq4

like image 717
kuak Avatar asked Jan 01 '23 01:01

kuak


2 Answers

They are not identical. The first character is different (although it looks identical to the naked eye)

Try

 print([ord(c) for c in a])
 print([ord(c) for c in b])
like image 70
blue_note Avatar answered Jan 08 '23 01:01

blue_note


If you can ignore small differences like this one, try:

from difflib import SequenceMatcher

word_1 = "comentar"

word_2 = " comentar"

result = SequenceMatcher(a=word_1, b=word_2).ratio() > 0.9

print(result)

This will return True

like image 40
Gokhan Gerdan Avatar answered Jan 08 '23 02:01

Gokhan Gerdan