Python unicode character codes?

Question:

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 By: tkbx

||

Answers:

Yes, with unicode character escapes:

print u'This is a full block: u2588'
Answered By: Eric

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

print u"Foou0020bar"

Prints

Foo bar
Answered By: nkr

You can use u to escape a Unicode character code:

s = u'u0020'

which defines a string containing the space character.

Answered By: David Heffernan
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.