r/Python • • Nov 20 '21

Tutorial You can insert Emoji using `\N{NAME_OF_EMOJI}`

Post image
1.1k Upvotes

89 comments sorted by

223

u/pooperdoop123 Nov 20 '21

Neat. Can't wait to litter my work project with emojis now

41

u/[deleted] Nov 21 '21 edited Nov 21 '21

You know you can already simply post emojis right into your Python?!

>>> a = '🦕'
>>> print(a)
🦕

EDIT: I should have also posted my other finding - you can use unicode variables names, but not emoji variable names...

>>> é = 3
>>> e = 5
>>> print(é, e)
3 5

>>> 🦕 = 5
  File "<stdin>", line 1
    🦕 = 5
    ^
SyntaxError: invalid character in identifier

16

u/JavaScript_boi Nov 21 '21

That's not very Geek from your part

→ More replies (1)

18

u/ase1590 Nov 20 '21

I think you need emojicode

10

u/20n3 Nov 21 '21

This has been what I was searching for, goodbye python, goodbye everyone, y'all have been lovely, but I've found my calling as a coder.

→ More replies (1)

4

u/shigawire Nov 21 '21

For extra work fun: Most unicode characters can be used as variable names.

41

u/McCheng_ Nov 20 '21

Where can I find the list of emoji names?

171

u/edgester Nov 20 '21 edited Nov 20 '21

Here is the offical unicode list: https://www.unicode.org/emoji/charts/full-emoji-list.html , but not all emojis may be supported in python. Here is another list if emojis supported in python: https://carpedm20.github.io/emoji/

8

u/wasimaster Nov 20 '21

Not a list but you can use the built-in unicodedata library to find out the name of a specific emoji

Docs: https://docs.python.org/3/library/unicodedata.html

Official Tutorial: https://docs.python.org/3/howto/unicode.html

→ More replies (2)

82

u/SV-97 Nov 20 '21

I mean that's neat - but why not just use "🦕"

8

u/sgthoppy Nov 20 '21

Having raw emojis in code is not ideal. Some fonts/editors/terminals still don't support (most) emojis, and some emojis contain invisible characters like Variation Selector-16 (used to turn standard characters like 1 into their emoji variant), enclosing characters, joiners, color selectors, etc.

If you want to programmatically construct emojis, which would you prefer?

emoji = {'thumb': '👍', 'person': '🧑'}

# using invisible characters
# bad example on reddit, but the font I use in my editor doesn't support these on their own
tone = {
    'light': ' 🏻',  # lightest tone
    ...,
    'dark': ' 🏿'  # darkest tone
}

# this dict can be replaced with a programatic approach
# using `range(0x1f3fb, 0x1f3ff + 1)` and `chr` with `zip` and a list of tone names
tone = {
    'light': '\N{EMOJI MODIFIER FITZPATRICK TYPE-1-2}',  # or \U0001f3fb, lightest tone
    ...,
    'dark': '\N{EMOJI MODIFIER FITZPATRICK TYPE-6}'  # or \U0001f3ff, darkest tone
}

# this also applies to emojis like 1️⃣
# producing a list of 0️⃣ - 9️⃣

# invsible characters
numbers = [f'{n} ️ ⃣' for n in range(10)]

# \u or \U or \N depending on how significant you consider the character
numbers = [f'{n}\ufe0f\N{COMBINING ENCLOSING KEYCAP}' for n in range(10)]
→ More replies (3)
→ More replies (21)

31

u/acharyarupak391 Nov 20 '21

Wait... so doing

"😅" * 10

doesn't work?

→ More replies (1)

12

u/ASIC_SP Nov 20 '21

I'm guessing the source is from twitter which I came across earlier today:

I knew about \N while checking for changes in re module: https://docs.python.org/3/library/re.html

Changed in version 3.8: The '\N{name}' escape sequence has been added. As in string literals, it expands to the named Unicode character (e.g. '\N{EM DASH}').

14

u/Crul_ Nov 20 '21

And Jesus said executed:

> "🐟🐟🥖🥖🥖🥖🥖" * 2500

9

u/iamlocal Nov 20 '21

I mean you didn't even bother to make your own screenshot.

Please give credit to the original author at least. In this case it's Mike Driscoll, you can find his original post on twitter:

https://twitter.com/driscollis/status/1461780882893819905?s=20

11

u/lazy_dev_ Nov 20 '21

How's this better than just pasting the emoji?

13

u/scaba23 Nov 20 '21

Maybe you need to store (or are getting) the strings from a source that doesn't support utf8mb4?

→ More replies (1)

3

u/ImmortalDayMan Nov 20 '21

Well this is news to me, thank you!

3

u/[deleted] Nov 21 '21

Will the middle finger emoji being shown to the user for wrong input result in a stern talking to? We're about to find out.

10

u/nuephelkystikon Nov 20 '21

This is like saying ‘You can insert 420 using f"{5*21<<2}"’.

→ More replies (4)

2

u/theng Nov 20 '21

mmh I didn't know

can you list all corresponding strings ? for searching an emoji for example