Reputation: 43
Sorry, this is likely a simple solution. In a server response, I am trying to return an emoji. Right now, I have this line:
return [b"Hello World " “👋”.encode(“utf-8”)]
However, I get the following error:
return [b"Hello World " “�👋”.encode(“utf-8”)]
^
SyntaxError: invalid character in identifier
What I'd like to see is: Hello World 👋
Upvotes: 1
Views: 2588
Reputation: 9523
There are several problems. Your code:
return [b"Hello World " “👋”.encode(“utf-8”)]
You see, you are using “
and ”
twice, instead of the proper double quote character ("
). You should use a proper code editor, or disable character conversion. You should care also on copy and paste.
As you see from the error, the problem is not the emoji on the string, but a problem of identifier (and it is a syntax error), so unknown characters outside string.
But if you correct to:
return [b"Hello World " "👋".encode("utf-8")]
you still have an error: SyntaxError: cannot mix bytes and nonbytes literals
.
This because Python will merge strings before to call the function encode
, but one is a b-string and the other it is a normal string.
So you could use one of the following:
return [b"Hello World " + "👋".encode("utf-8")] # this force order of operator
or the following two (that are equivalent).
return [b"Hello World " "👋"]
return [b"Hello World 👋"]
Python3 uses UTF-8 as default source encoding, so your editor will already encode the emoji into UTF-8, so you can use it in a b-string (UTF-8 encoded).
Note: this is not a very safe assumption: one could manually force source code to be in other encodings, but in that case, you will have probably also problem with the first method (saving a file with emoji in other encodings).
Upvotes: 0
Reputation: 5817
In Python, you can put (almost) any Unicode character in a string literal.
Also, you can use most Unicode letters in identifiers, eg. if you think it's appropriate to define a variable α
(Greek lower-case alpha).
But you can't use "fancy quotes" for delimiting string literals.
Look carefully: the double quotes around the emoji (and also around utf-8
) aren't straight ASCII quotes, but rather typographical ones – the ones word processors substitute when typing a double quote in text.
Make sure you use a proper programming editor or an IDE for coding. Then the line will look like this:
return [b"Hello World " "👋".encode("utf-8")]
I realise that this still doesn't work: you cannot mix byte string and Unicode string literals (even though here the Unicode literal gets converted to bytes
later).
Instead, you have to concatenate them with the +
operator:
return [b"Hello World " + "👋".encode("utf-8")]
Or use a single string literal, like tripleee suggested.
Upvotes: 1
Reputation: 189307
The problem is that a byte string b'...'
cannot contain a character which does not fit into a byte. But you don't need a byte string here anyway; encode
will convert a string to bytes - that's what it does.
Try
return ["Hello World “👋”".encode("utf-8")]
The quotes in your question were wacky; I assume you want curly quotes around the emoji and honest-to-GvR quotes around the Python string literals.
Upvotes: 2