Daniel Tremer
Daniel Tremer

Reputation: 200

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!

Upvotes: 0

Views: 220

Answers (1)

User2342342351
User2342342351

Reputation: 2174

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.

Upvotes: 1

Related Questions