DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Edited on

String in Python (6)

Buy Me a Coffee

*Memo:

<Decode a byte string to a string>:

print(str(object=b'', encoding='utf-8', errors='strict')) print(str(encoding='utf-8', errors='strict')) print(str(encoding='utf-8')) print(str(errors='strict')) # Nothing 
Enter fullscreen mode Exit fullscreen mode
v = "Lёт's gφ!" # Let's go!  b = v.encode(encoding='utf-8') print(b) # b"L\xd1\x91\xd1\x82's g\xcf\x86!"  print(str(object=b, encoding='utf-8')) # Lёт's gφ! 
Enter fullscreen mode Exit fullscreen mode
v = "Lёт's gφ!" # Let's go!  b = v.encode(encoding='utf-7') print(b) # b"L+BFEEQg's g+A8Y!"  print(str(object=b, encoding='utf-7')) # Lёт's gφ! 
Enter fullscreen mode Exit fullscreen mode
v = "Lёт's gφ!" # Let's go!  b = v.encode(encoding='utf-16') print(b) # b"\xff\xfeL\x00Q\x04B\x04'\x00s\x00 \x00g\x00\xc6\x03!\x00"  print(str(object=b, encoding='utf-16')) # Lёт's gφ! 
Enter fullscreen mode Exit fullscreen mode
v = "Lёт's gφ!" # Let's go!  b = v.encode(encoding='big5') print(b) # b"L\xc7\xce\xc7\xdb's g\xa3p!"  print(str(object=b, encoding='big5')) # Lёт's gφ! (Let's go!) 
Enter fullscreen mode Exit fullscreen mode
v = "Lёт's gφ!" # Let's go!  b = v.encode(encoding='ascii', errors='ignore') print(b) # b"L's g!"  print(str(object=b, encoding='ascii', errors='ignore')) # L's g! 
Enter fullscreen mode Exit fullscreen mode
v = "Lёт's gφ!" # Let's go!  b = v.encode(encoding='ascii', errors='replace') print(b) # b"L??'s g?!"  print(str(object=b, encoding='ascii', errors='replace')) # L??'s g?! 
Enter fullscreen mode Exit fullscreen mode
v = "Lёт's gφ!" # Let's go!  b = v.encode(encoding='ascii', errors='xmlcharrefreplace') print(b) # b"L&#1105;&#1090;'s g&#966;!"  print(str(object=b, encoding='ascii', errors='xmlcharrefreplace')) # L&#1105;&#1090;'s g&#966;! 
Enter fullscreen mode Exit fullscreen mode
v = "Lёт's gφ!" # Let's go!  b = v.encode(encoding='ascii', errors='backslashreplace') print(b) # b"L\\u0451\\u0442's g\\u03c6!"  print(str(object=b, encoding='ascii', errors='backslashreplace')) # L\u0451\u0442's g\u03c6! 
Enter fullscreen mode Exit fullscreen mode
import codecs def hashreplace_handler(x): return ((x.end - x.start) * '#', x.end) codecs.register_error('hashreplace', hashreplace_handler) v = "Lёт's gφ!" # Let's go!  b = v.encode(encoding='ascii', errors='hashreplace') print(b) # b"L##'s g#!"  print(str(object=b, encoding='ascii', errors='hashreplace')) # L##'s g#! 
Enter fullscreen mode Exit fullscreen mode
v = "Lёт's gφ!" # Let's go!  b = v.encode(encoding='ascii', errors='strict') # UnicodeEncodeError: 'ascii' codec can't encode characters # in position 1-2: ordinal not in range(128) 
Enter fullscreen mode Exit fullscreen mode
import codecs def hashreplace_handler(x): return ((x.end - x.start) * '#', x.end) codecs.register_error('hashreplace', hashreplace_handler) v = "Lёт's gφ!" # Let's go!  b = v.encode(encoding='utf-8', errors='strict') print(b) # b"L\xd1\x91\xd1\x82's g\xcf\x86!"  print(str(object=b, encoding='ascii', errors='ignore')) # L's g!  print(str(object=b, encoding='ascii', errors='replace')) # L����'s g��!  print(str(object=b, encoding='ascii', errors='backslashreplace')) # L\xd1\x91\xd1\x82's g\xcf\x86!  print(str(object=b, encoding='ascii', errors='hashreplace')) # L####'s g##!  print(str(object=b, encoding='ascii', errors='strict')) # UnicodeDecodeError: 'ascii' codec can't decode byte 0xd1 # in position 1: ordinal not in range(128)  print(str(object=b, encoding='ascii', errors='xmlcharrefreplace')) # TypeError: don't know how to handle UnicodeDecodeError in error callback 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)