wstyczen
wstyczen

Reputation: 17

Convert a string unicode number into represented unicode character

I wrote a function that returns a generated unicode nr string:

    def __str__(self):
     return '0001F0' + chr(ord('A')+self.color.value-1) + \
        hex(self.value.value).lstrip('0x').rstrip('L')

Result:

print(Card(Color(1), Value(2))) #'0001F0A2'

But I can't figure out how to print out the unicode char it represents, ie. the result you would get with:

print('\U0001F0A2')

Trying to just add the prefix '\U' like this:

    def __str__(self):
    return '\U' + '0001F0' + chr(ord('A')+self.suit.value-1) + \
        hex(self.rank.value).lstrip('0x').rstrip('L')

Throws an error:

    return '\U' + '0001F0' + chr(ord('A')+self.suit.value-1) + \
                ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: truncated \UXXXXXXXX escape

I haven't been able to find the solution online and the things I tried haven't worked. Thanks for the help in advance.

Upvotes: 0

Views: 58

Answers (1)

May be this will help:

def f(x):
    return chr(int(x, 16))
f('1F0A2')

output:

'🂢' #'PLAYING CARD TWO OF SPADES'

Upvotes: 1

Related Questions