Python比较2个相同的字符串返回“False”

2024-09-29 19:13:47 发布

您现在位置:Python中文网/ 问答频道 /正文

当我比较这两个字符串时,得到的值是False。在

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

我怎么能修好这个?我尝试过改变两个字符串的编码,但没有任何效果。在

你可以在这里试试:https://onlinegdb.com/HJ8xYLPq4


Tags: 字符串httpscomfalse编码print效果onlinegdb
2条回答

它们不完全相同。第一个字符是不同的(尽管它看起来和肉眼一样)

试试看

 print([ord(c) for c in a])
 print([ord(c) for c in b])

如果您可以忽略像这样的小差异,请尝试:

from difflib import SequenceMatcher

word_1 = "comentar"

word_2 = " comentar"

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

print(result)

这将返回True

相关问题 更多 >

    热门问题