Search code examples
pythonlistencodingutf-8letters

Python - how do I create a list of letters from a string with special characters?


I want to create a list of letters from this string in python:

string = 'utf✮fff'

I have tried this:

>>> string = "utf✮fff"
>>> print list(string)
['u', 't', 'f', '\xe2', '\x9c', '\xae', 'f', 'f', 'f']

However it should look like this:

['u', 't', 'f', '\xe2\x9c\xae', 'f', 'f', 'f']

Does anyone know how to create this output? Thank you in advance!


Solution

  • You have to use unicode strings:

    string = u'utf✮fff'
    print list(string)
    
    [u'u', u't', u'f', u'\u272e', u'f', u'f', u'f']
    
    
    string = 'utf✮fff'
    string = unicode(string)
    print list(string)
    
    [u'u', u't', u'f', u'\u272e', u'f', u'f', u'f']
    

    Note that in your case, you will have to set the

    # coding: utf8
    

    header.