15

I want to print some unicode characters but u'\u1000' up to u'\u1099'. This doesn't work:

for i in range(1000,1100):
    s=unicode('u'+str(i))
    print i,s
Tim Cooper
  • 151,519
  • 37
  • 317
  • 271
Dirk Nachbar
  • 707
  • 3
  • 9
  • 20
  • Three of the answers here are functionally identical, posted within minutes of each other. Also, while I know this asks for unicode in a certain range, in case anyone came here looking to print the full range, this functionally identical answer [over here](https://stackoverflow.com/a/33043329/1397555) (and a duplicate question too) gives that. – Alex Hall Aug 10 '17 at 17:02

7 Answers7

17

You'll want to use the unichr() builtin function:

for i in range(1000,1100):
    print i, unichr(i)

Note that in Python 3, just chr() will suffice.

Sanqui
  • 186
  • 4
11

Use unichr:

s = unichr(i)

From the documentation:

unichr(i)

Return the Unicode string of one character whose Unicode code is the integer i. For example, unichr(97) returns the string u'a'.

Mark Byers
  • 767,688
  • 176
  • 1,542
  • 1,434
7

Try the following:

for i in range(1000, 1100):
    print i, unichr(i)
Andrew Clark
  • 192,132
  • 30
  • 260
  • 294
6

unichr is the function you are looking for - it takes a number and returns the Unicode character for that point.

for i in range(1000, 1100):
    print i, unichr(i)
Sean Vieira
  • 148,604
  • 32
  • 306
  • 290
3

(Python 3) The following will give you the characters corresponding to an arbitrary unicode range

start_code, stop_code = '4E00', '9FFF'  # (CJK Unified Ideographs)
start_idx, stop_idx = [int(code, 16) for code in (start_code, stop_code)]  # from hexadecimal to unicode code point
characters = []
for unicode_idx in range(start_idx, stop_idx+1):
    characters.append(chr(unicode_idx))
Bruno Degomme
  • 459
  • 4
  • 9
0

Use chr instead of unichr to avoid an error message.

for i in range(1000, 1100):
    print i, chr(i)
Eduardo Freitas
  • 763
  • 6
  • 6
-1

One might appreciate this php-cli version:

It is using html entities and UTF8 decoding.

Recent version of XTERM and others terminals supports unicode chars pretty nicely :)

php -r 'for ($x = 0; $x < 255000; $x++) {echo html_entity_decode("&#".$x.";",ENT_NOQUOTES,"UTF-8");}'

enter image description here

NVRM
  • 8,789
  • 1
  • 69
  • 80