How to put emoji in a string?

Asked

Viewed 198 times

7

I know that in a string I can put any characters you want, ex:

string = "qualquer coisa! @#$%ª太陽"

But what if I want to put one emoji in a string, how do I? Has any specific way?

Can I put it right into the code that works? Because I did it and it worked:

emoji = '' # criei uma string contendo um emoji

Is that right or did it work by coincidence? If not, there is another way to do it?

1 answer

8

in Python from "3" - everything internally is Unicode. Unicode is translated by an "encoding' when data is sent to an API - in general they all work with "utf-8". Utf-8 is a specific encoding that supports all Unicode characters (in contrast to Windows native encoding for example, which only supports 256 characters).

In other words: if you simply paste the quoted emoji into your source code, it will work just like any other string. If you don’t want to leave emojis in your source code, you can leave them escaped in the source code with the " uXXXX" notation where "XXXX" is the code of the Emoji Unicode-Codepoint - (for example " u263a" for " "). If the code has more than 5 digits, then you need to use \U capital and 8 digits: "\U0001f389" to ''. (no matter if it is 5 digits - it has to be 8 hexadecimal digits after the \U: what is missing must be completed with 0 to the left, as in this example)

  • 1

    if the code has 5X type u1F389

  • 1

    @PatrícioGabriel https://ideone.com/QdgCAu

  • 3

    updated the answer with an example of characters where Codepoint needs more than 4 hex digits.

  • @Initially I voted to close because it wasn’t clear (at least for me and the others) that you just wanted to know how to put an emoji in a string (after all, the question mentioned a "tag", but there was no tag in the code). Perhaps it was precious of me, but I hope you understand that the idea of the site is that doubts are also useful for future visitors, so it has to be very clear, no doubt or interpretations. Anyway, I edited the question and voted to reopen...

  • 1

    ...see if the doubt was just that (but by feedback in this answer, it seems to be). Another tip is: if you put "researched and I didn’t find", say at least what you researched (after all, a search for "python string emoji" already brings several results), and say why what you thought didn’t fit your case. This helps to focus doubt and avoids closure. The site may seem "hostile" at first, especially for those who do not know its - complicated, I admit - functioning, but it is aimed at creating a repository of knowledge (it is something that goes beyond "individual help")...

  • ...Sometimes we can make mistakes and even exaggerate rigidity, but it is with the best of intentions. Anyway, now all that’s missing is a vote of reopening, sorry for any inconvenience, and I hope you understand that it was with the best of intentions. Finally, there is the tip to read this guide

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.