1

My title terms might not be correct and may be a reason why I can't find this simple thing from websites.

I have a list of string variables. How do I actually concatenate them and output a real unicode sentence in Python?

base = ['280', '281', '282', '283']
end = ['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f']
unicodes = [u''.join(['\u', j, i]) for j in base for i in end]

for u in unicodes:
    print u

I will get only strings like '\u280F' but not real character. But if I do:

print u'\u280F'

correct symbols shows up, which is: ⠏

And I'm sure there is a more elegant way to get a range of the symbols from u2800 to u283F...

0

2 Answers 2

5

Conver the strings to integers (using int with base 16), the use unichr (chr if you're using Python 3.x) to convert the number into unicode object.

>>> int('280' + 'F', 16)  # => 0x280F, 16: hexadecimal
10255
>>> unichr(int('280' + 'F', 16))  # to unicode object
u'\u280f'
>>> print unichr(int('280' + 'F', 16))
⠏

base = ['280', '281', '282', '283']
end = ['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f']
unicodes = [unichr(int(j + i, 16)) for j in base for i in end]

for u in unicodes:
    print u
Sign up to request clarification or add additional context in comments.

Comments

0

If you are stuck with unicodes input; you could use unicode-escape codecs, to get Unicode (b'\\u2800'.decode('unicode-escape') == u'\u2800'):

>>> for escaped in unicodes: print escaped.decode('unicode-escape')
...
⠽
⠾
⠿

Otherwise, generate range of integers directly:

for ordinal in range(0x2800, 0x283f + 1):
    print unichr(ordinal)

It produces the same output in this case.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.