how to split a unicode string into list

PersianGulf picture PersianGulf · Sep 10, 2013 · Viewed 33.2k times · Source

I have the following code:

stru = "۰۱۲۳۴۵۶۷۸۹"
strlist = stru.decode("utf-8").split()
print strlist[0]

my output is :

۰۱۲۳۴۵۶۷۸۹

But when i use:

print strlist[1]

I get the following traceback:

IndexError: list index out of range

My question is, how can I split my string? Of course, remember I get my string from a function, consider it's a variable?

Answer

Ignacio Vazquez-Abrams picture Ignacio Vazquez-Abrams · Sep 10, 2013
  1. You don't need to.

    >>> print u"۰۱۲۳۴۵۶۷۸۹"[1]
    ۱
    
  2. If you still want to...

    >>> list(u"۰۱۲۳۴۵۶۷۸۹")
    [u'\u06f0', u'\u06f1', u'\u06f2', u'\u06f3', u'\u06f4', u'\u06f5', u'\u06f6', u'\u06f7', u'\u06f8', u'\u06f9']