10

Is there a way to "insert" a Unicode character into a string in Python 3? For example,

>>>import unicode
>>>string = 'This is a full block: %s' % (unicode.charcode(U+2588))
>>>print(string)
This is a full block: █
asked Nov 3, 2012 at 21:54
1
  • 1
    Your example uses a code point, but you say "character". You should be aware that what most people mean when they say character can correspond to several code points. Commented Nov 3, 2012 at 21:58

3 Answers 3

18

Yes, with unicode character escapes:

print u'This is a full block: \u2588'
answered Nov 3, 2012 at 21:57
Sign up to request clarification or add additional context in comments.

6 Comments

... or with unichr(), if you want to create a character from a dynamic code point (doesn't seem to be the case here; I added it for completeness).
If you prefer something more readable, you can use u'This is a full block: \N{FULL BLOCK}'.
Where can I find a list of that names like FULL BLOCK?
Is the number you put after the \u decimal, hexadecimal, octal, or what exactly?
|
0

You can use \udddd where you replace dddd with the the charcode.

print u"Foo\u0020bar"

Prints

Foo bar
answered Nov 3, 2012 at 21:57

Comments

0

You can use \u to escape a Unicode character code:

s = u'\u0020'

which defines a string containing the space character.

answered Nov 3, 2012 at 21:58

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.