Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Jupyter Notebook: How to print unicode characters from wikipedia hex value (like U+1F0A1)?

Tags:

python

unicode

I would like to use the playing card characters in unicode within a jupyter notebook.

Unicode codes are here. https://en.wikipedia.org/wiki/Playing_cards_in_Unicode

Printing the suits works

print('\u2660')

returns

ā™ 

As an example, the Ace of Spades šŸ‚” unicode is U+1F0A1.

I can paste that character and print it.

print('šŸ‚”') 

And I can encode this

'šŸ‚”'.encode('utf-8')

b'\xf0\x9f\x82\xa1'

But how do I print this by the code "U+1F0A1" from wikipedia?

like image 588
576i Avatar asked Oct 19 '25 07:10

576i


1 Answers

There is another type of escape code (capital U) that requires eight digits:

>>> print('\U0001F0A1')
šŸ‚”

You can also print by converting a number:

>>> chr(0x1f0a1)
'šŸ‚”'
>>> print(chr(0x1f0a1))
šŸ‚”

So you can programmatically generate a 52-card desk as:

>>> suit = 0x1f0a0,0x1f0b0,0x1f0c0,0x1f0d0
>>> rank = 1,2,3,4,5,6,7,8,9,10,11,13,14
>>> for s in suit:
...     for r in rank:
...         print(chr(s+r),end='')
...     print()
... 
šŸ‚”šŸ‚¢šŸ‚£šŸ‚¤šŸ‚„šŸ‚¦šŸ‚§šŸ‚ØšŸ‚©šŸ‚ŖšŸ‚«šŸ‚­šŸ‚®
šŸ‚±šŸ‚²šŸ‚³šŸ‚“šŸ‚µšŸ‚¶šŸ‚·šŸ‚øšŸ‚¹šŸ‚ŗšŸ‚»šŸ‚½šŸ‚¾
šŸƒšŸƒ‚šŸƒƒšŸƒ„šŸƒ…šŸƒ†šŸƒ‡šŸƒˆšŸƒ‰šŸƒŠšŸƒ‹šŸƒšŸƒŽ
šŸƒ‘šŸƒ’šŸƒ“šŸƒ”šŸƒ•šŸƒ–šŸƒ—šŸƒ˜šŸƒ™šŸƒššŸƒ›šŸƒšŸƒž
like image 68
Mark Tolonen Avatar answered Oct 20 '25 20:10

Mark Tolonen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!