'Why are the strings not equal after I slice out card suit emoji?

Python 3.9

cards = ['♥️A','♠️2','♣️3']
ref_list = ['A','2','3']

for a in cards:
    print(a[1:])
    print(a[1:] in ref_list)

the output is

A
False
️2
False
️3
False

How should I revise the code to make it work? (Make the output True?)



Solution 1:[1]

Welcome to the world of Unicode!

>>> len('??A')
3

>>> [hex(ord(c)) for c in '??A']
['0x2665', '0xfe0f', '0x41']

>>> '??A'[0:1]
'?'

>>> '??A'[0:2]
'??'

The red heart in this string is composed of the Black Heart Suit and the Variation Selector code points, so your complete string has 3 code points.

Consider not using string concatenation to represent this data, and define a dataclass or at least a tuple like ('??', 'A') instead.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1