Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python ♥️♠️♣️♦️ in list Card Deck and Poker Suit unicode question

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

Question: how should I revise the code to make it work ? (the output is True ?)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading