fork download
  1. if __name__ == '__main__':
  2. # define our Unicode string
  3. uni = u"Hello\u001A\u0BC3\u1451\U0001D10CUnicode"
  4.  
  5. # UTF-8 and UTF-16 can fully encode *any* unicode string
  6.  
  7. print "UTF-8", repr(uni.encode('utf-8'))
  8. print "UTF-16", repr(uni.encode('utf-16'))
  9.  
  10. # ASCII can only work with code values from 0-127. Below we tell Python
  11.  
  12. print "ASCII ", uni.encode('ascii','replace')
  13.  
  14. # ISO-8859-1 is similar to ASCII
  15.  
  16. print "ISO-8859-1 ", uni.encode('iso-8859-1','replace')
  17.  
  18. uni = uni.encode('utf-8')
  19. bstr = unicode(uni, 'utf-8')
  20. print "Back from UTF-8:", repr(bstr)
  21.  
Success #stdin #stdout 0.01s 7368KB
stdin
Standard input is empty
stdout
UTF-8 'Hello\x1a\xe0\xaf\x83\xe1\x91\x91\xf0\x9d\x84\x8cUnicode'
UTF-16 '\xff\xfeH\x00e\x00l\x00l\x00o\x00\x1a\x00\xc3\x0bQ\x144\xd8\x0c\xddU\x00n\x00i\x00c\x00o\x00d\x00e\x00'
ASCII  Hello???Unicode
ISO-8859-1  Hello???Unicode
Back from UTF-8: u'Hello\x1a\u0bc3\u1451\U0001d10cUnicode'