84

I've been looking through the Python Cookbook (2nd Edition) to learn how to process strings and characters.

I wanted to try converting a number into its Unicode equivalent. So I tried using the built-in function called 'unichr', which, according to the Cookbook, goes something like:

>>> print repr(unichr(8224))

... and will output:

u'\u2020'

However, the code failed. I thought it had something to do with print (because Python 3 uses print() instead of print ""), but that didn't work out as well. I tried several variations to the code, and it still failed. At long last, I just typed a simple line:

unichr(10000)

To my surprise, this error message kept popping up, no matter what value I put into the above function:

 NameError: name 'unichr' is not defined

What could be the problem? Is there some specific module that I'm supposed to import?

Mike T
  • 41,085
  • 18
  • 152
  • 203
xax
  • 2,043
  • 4
  • 24
  • 28
  • 3
    Just a tip: you could and should have mentioned the error message right after "the code failed." Always note the error message Python gives (and the *exact* error message, not from memory), and mention it early. – Jürgen A. Erhard Feb 28 '10 at 18:28
  • Thank you for the advice, I will definitely keep it mind. – xax Feb 28 '10 at 18:33

5 Answers5

111

In Python 3, you just use chr:

>>> chr(10000)
'✐'
phoenix
  • 7,988
  • 6
  • 39
  • 45
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
23

In Python 3, there's no difference between unicode and normal strings anymore. Only between unicode strings and binary data. So the developers finally removed the unichr function in favor of a common chr which now does what the old unichr did. See the documentation here.

AndiDog
  • 68,631
  • 21
  • 159
  • 205
  • There are still two string-ish types. What 2.x calls `unicode`, 3.x calls `str` (with trivial changes). What 2.x calls `str`, 3.x calls `bytes` (with moderate changes). – Mike Graham Feb 28 '10 at 18:47
7

In case you need to run in both python 2 and python 3, you can use this common syntax (the unused syntax would point to the new one)

try:
    unichr
except NameError:
    unichr = chr
mork
  • 1,747
  • 21
  • 23
6

Python 3.x doesn't have a special Unicode string type/class. Every string is a Unicode string. So... I'd try chr. Should give you what unichr did pre-3.x. Can't test, sadly.

Jürgen A. Erhard
  • 4,908
  • 2
  • 23
  • 25
6

As suggestted by this is a better way to do it for it is compatible for both 2 and 3:

# Python 2 and 3:
from builtins import chr
assert chr(8364) == '€'

And you may need pip install future incase some error occures.

shellbye
  • 4,620
  • 4
  • 32
  • 44