a gift function and a question

Steven D'Aprano steve+comp.lang.python at pearwood.info
Tue Sep 10 11:11:21 EDT 2013


On Tue, 10 Sep 2013 07:01:20 +0000, Steven D'Aprano wrote:

> On Tue, 10 Sep 2013 00:40:59 +0430, Mohsen Pahlevanzadeh wrote:

>> My question is , do you have reverse of this function?
>> persianToInteger?
> 
> The Python built-in int function already supports that:
> 
> 
> py> int('۳۴۵۵')
> 3455


Oh, I forgot to mention, this works back to at least Python 2.4, provided 
you remember to use Unicode strings rather than bytes:


py> b = '۳۴۵۵'  # This fails.
py> int(b)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '\xdb\xb3\xdb\xb4\xdb
\xb5\xdb\xb5'
py>
py> s = u'۳۴۵۵'  # This works.
py> int(s)
3455



-- 
Steven



More information about the Python-list mailing list