Home > Blockchain >  Why are these dots not equal in python?
Why are these dots not equal in python?

Time:05-25

I am working on texts and have the left dot from an input text and right dot typed from a keyboard. However, in Python, they are not being treated as equal.

'․' == '.'
Out[870]: False

What could be a possible reason, and how can I recreate the left dot using the keyboard?

CodePudding user response:

The dot on the left is not a period: it is a one dot leader Unicode character.

In Python, you can print it by using "\u2024":

print('\u2024')

This outputs:

You can use this for comparison purposes as well. If you do:

print('․' == '\u2024')

it will output

True

CodePudding user response:

I would suggest going with str.maketrans in this case:

trans_table = str.maketrans({'․': '.'})
print(trans_table)

# prints `False`
print('․' == '.')

# now prints `True`
print('․'.translate(trans_table) == '.')
# or:
#   '․'.replace('․', '.') == '.'

CodePudding user response:

Basically, the visual representation of characters as we perceive them has no meaning from a machine's perspective.

Python compares the unicode values of those two values, and those obviously differ.

The dot on the left is "\u2024", whereas the dot on the right is "\u2e".

  • Related