#TIL : String Format Unicode params

Dec 1, 2015 4 mins read 8d4302659

String Format Unicode params

unicode_thing = u"Xin chào mọi người"
a = '{}'.format(unicode_thing)

will cause the error UnicodeEncodeError: 'ascii' codec can't encode character u'\xe0' in position 6: ordinal not in range(128)

The solution is add u prefix the pattern (it means using unicode pattern) :

unicode_thing = u"Xin chào mọi người"
a = u'{}'.format(unicode_thing)
comments powered by Disqus