Accessing Bangla (UTF-8) string by index in Python

2019-08-29 18:53发布

问题:

I have a string in Bangla and I'm trying to access characters by index.

# -*- coding: utf-8 -*-
bstr = "তরদজ"
print bstr # This line is working fine
for i in bstr:
    print i, # question marks are printed

I don't know why it isn't working.

回答1:

Turn it into unicode:

>>> bstr = "তরদজ"
>>> for i in bstr.decode('utf-8'):
...     print i
... 
ত
র
দ
জ