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: █
M.J. Rayburn
5395 silver badges15 bronze badges
asked Nov 3, 2012 at 21:54
tkbx
16.4k34 gold badges93 silver badges123 bronze badges
-
1Your 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.user395760– user3957602012年11月03日 21:58:59 +00:00Commented Nov 3, 2012 at 21:58
3 Answers 3
Yes, with unicode character escapes:
print u'This is a full block: \u2588'
answered Nov 3, 2012 at 21:57
Eric
98.1k54 gold badges257 silver badges389 bronze badges
Sign up to request clarification or add additional context in comments.
6 Comments
Sven Marnach
... 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).Eryk Sun
If you prefer something more readable, you can use
u'This is a full block: \N{FULL BLOCK}'.buhtz
Where can I find a list of that names like
FULL BLOCK?denis
@buhtz, en.wikipedia.org/wiki/Block_Elements lists blocks; en.wikipedia.org/wiki/Unicode_symbols -> decodeunicode.org/de/u+2580 . HTH
Toothpick Anemone
Is the number you put after the
\u decimal, hexadecimal, octal, or what exactly? |
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
nkr
3,0587 gold badges33 silver badges39 bronze badges
Comments
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
David Heffernan
616k46 gold badges1.1k silver badges1.5k bronze badges
Comments
lang-py