Skip to content

Instantly share code, notes, and snippets.

@walterrenner
Created October 11, 2017 12:26
Show Gist options
  • Save walterrenner/0a96ee55125b5070807385880cf5d9f0 to your computer and use it in GitHub Desktop.
Save walterrenner/0a96ee55125b5070807385880cf5d9f0 to your computer and use it in GitHub Desktop.
Dealing with unicode and strings in Python
# Deal exclusively with unicode objects as much as possible
# by decoding things to unicode objects when you first get them and
# encoding them as necessary on the way out.
# https://stackoverflow.com/a/6048203
>>> s = 'abc'
>>> type(s)
<type 'str'>
>>> u = u'abc' # note the u prefix
>>> type(u)
<type 'unicode'>
# convert unicode to string by encoding
>>> s = u.encode('utf8')
>>> type(s)
<type 'str'>
# convert string to unicode by decoding
>>> u1 = s.decode('utf-8')
>>> type(u1)
<type 'unicode'>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment