if __name__ == '__main__':
# define our Unicode string
uni = u"Hello\u001A\u0BC3\u1451\U0001D10CUnicode"
# UTF-8 and UTF-16 can fully encode *any* unicode string
print "UTF-8", repr(uni.encode('utf-8'))
print "UTF-16", repr(uni.encode('utf-16'))
# ASCII can only work with code values from 0-127. Below we tell Python
print "ASCII ", uni.encode('ascii','replace')
# ISO-8859-1 is similar to ASCII
print "ISO-8859-1 ", uni.encode('iso-8859-1','replace')
uni = uni.encode('utf-8')
bstr = unicode(uni, 'utf-8')
print "Back from UTF-8:", repr(bstr)
aWYgX19uYW1lX18gPT0gJ19fbWFpbl9fJzoKICAgICMgZGVmaW5lIG91ciBVbmljb2RlIHN0cmluZwogICAgdW5pID0gdSJIZWxsb1x1MDAxQVx1MEJDM1x1MTQ1MVxVMDAwMUQxMENVbmljb2RlIgoKICAgICMgVVRGLTggYW5kIFVURi0xNiBjYW4gZnVsbHkgZW5jb2RlICphbnkqIHVuaWNvZGUgc3RyaW5nCgogICAgcHJpbnQgIlVURi04IiwgcmVwcih1bmkuZW5jb2RlKCd1dGYtOCcpKQogICAgcHJpbnQgIlVURi0xNiIsIHJlcHIodW5pLmVuY29kZSgndXRmLTE2JykpCgogICAgIyBBU0NJSSBjYW4gb25seSB3b3JrIHdpdGggY29kZSB2YWx1ZXMgZnJvbSAwLTEyNy4gQmVsb3cgd2UgdGVsbCBQeXRob24KCiAgICBwcmludCAiQVNDSUkgIiwgdW5pLmVuY29kZSgnYXNjaWknLCdyZXBsYWNlJykKCiAgICAjIElTTy04ODU5LTEgaXMgc2ltaWxhciB0byBBU0NJSQoKICAgIHByaW50ICJJU08tODg1OS0xICIsIHVuaS5lbmNvZGUoJ2lzby04ODU5LTEnLCdyZXBsYWNlJykKCiAgICB1bmkgPSB1bmkuZW5jb2RlKCd1dGYtOCcpCiAgICBic3RyID0gdW5pY29kZSh1bmksICd1dGYtOCcpCiAgICBwcmludCAiQmFjayBmcm9tIFVURi04OiIsIHJlcHIoYnN0cikK