How can I create a Python tuple of Unicode strings?

PythonProgramming

You can create a tuple of unicode strings in python using the u'' syntax when defining this tuple. 

example

a = [(u'亀',), (u'犬',)]
print(a)

Output

This will give the output

[('亀',), ('犬',)]

Note that you have to provide the u if you want to say that this is a unicode string. Else it will be treated as a normal binary string. And you'll get an unexpected output. 

example

a = [('亀',), ('犬',)]
print(a)

Output

This will give the output

[('\xe4\xba\x80',), ('\xe7\x8a\xac',)]
raja
Published on 18-Apr-2018 14:05:32
Advertisements