Skip to content

Instantly share code, notes, and snippets.

@arthuralvim
Created July 8, 2015 03:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arthuralvim/9d3c0bf59e8860ba395b to your computer and use it in GitHub Desktop.
Save arthuralvim/9d3c0bf59e8860ba395b to your computer and use it in GitHub Desktop.
About unicode and str in Python.
# unicode => human readable format
uni_text = u'café'
print type(uni_text)
print isinstance(uni_text, str)
print len(uni_text)
uni_str_convert = uni_text.encode('UTF-8')
# str => bytes
str_text = 'café'
print type(str_text)
print isinstance(str_text, unicode)
print len(str_text)
str_uni_convert = str_text.decode('UTF-8')
# comparisons
print uni_str_convert == uni_text
print uni_str_convert == str_text
print str_uni_convert == uni_text
print str_uni_convert == str_text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment