0

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

user3840170
  • 26,597
  • 4
  • 30
  • 62
skksdd
  • 3
  • 2
  • What does the sentence 'make it work' mean? In other terms, what is the expected output? Please read [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask). – Nilton Moura Apr 13 '22 at 04:12
  • The point is '♥️A'[1:] the output is 'A' , but this 'A' is not identical to the str 'A'. I cannot tell the difference by eye. Maybe there's some harsh difference. – skksdd Apr 13 '22 at 04:25

1 Answers1

4

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.

Koterpillar
  • 7,883
  • 2
  • 25
  • 41