Python - Comparing 2 identical strings returns 'False'

Refresh

April 2019

Views

22 time

2

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

3 answers

0

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

0

There is a space in front of the word in b. If this isn't being used for some more complex code you can just delete the space and it will return True.

3

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])