DEV Community

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

Posted on • Edited on

Bytes in Python (3)

Buy Me a Coffee

*Memo:

bytes() can create a bytes with or without several types of values or can encode a string to a bytes as shown below:

*Memo to create a bytes:

  • The 1st argument is source(Optional-Default:b''-Type:Bytes-like object/Iterable(int)/int):
    • int gives a null value(\x00) which represents no value.

*Memo to encode a string to a bytes:

  • The 1st argument is source(Required-Type:str).
  • The 2nd argument is encoding(Required-Type:str):
    • 'utf-8', 'utf-7', 'utf-16', 'big5', 'ascii', etc can be set to it.
    • You can see Standard Encodings for more possible values.
  • The 3rd argument is errors(Optional-Default:'strict'-Type:str):
    • It controls encoding error with the error handlers, 'strict', 'ignore', 'replace', 'xmlcharrefreplace', 'backslashreplace', etc.
    • 'strict' raises UnicodeError if the character or byte, which cannot be encoded or decoded, exists.
    • 'ignore' ignores the character or byte which cannot be encoded or decoded.
    • 'replace' replaces the character or byte, which cannot be encoded or decoded, with ? for encoding or for decoding.
    • 'xmlcharrefreplace' replaces the character, which cannot be encoded, with the XML/HTML numeric character reference format &#num;:
      • It doesn't support decoding so error occurs for the byte which cannot be decoded while error doesn't occur for the byte which can be decoded.
    • 'backslashreplace' replaces the character or byte, which cannot be encoded or decoded, with the hexadecimal format \xhh, \uxxxx or \Uxxxxxxxx for encoding or \xhh for decoding.
    • You can see more error handlers.
    • You can create your own error handler with codecs.register_error().

<Create a bytes>:

v = bytes() # Empty bytes v = bytes(b'') # Empty bytes v = bytes(source=b'12') # bytes v = bytes(source=bytearray(b'12')) # bytearray v = bytes(source=12) # int v = bytes(source=True) # bool v = bytes(source=[1, 2, 3]) # list(int) v = bytes(source=(1, 2, 3)) # tuple(int) v = bytes(source={1, 2, 3}) # set(int) v = bytes(source=frozenset({1, 2, 3})) # frozenset(int) v = bytes(source={1:2, 3:4}) # dict(int:int) v = bytes(source={1:2, 3:4}.keys()) # dict(int:int).keys() v = bytes(source={1:2, 3:4}.values()) # dict(int:int).values() v = bytes(source=iter([1, 2, 3])) # iterator(int) v = bytes(source=range(10, 20)) # range # No error  print(type(bytes())) # <class 'bytes'>  v = bytes(source='12') # str v = bytes(source=1.2) # float v = bytes(source=1.2+3.4j) # complex v = bytes(source={1:2, 3:4}.items()) # dict(int:int).items() v = bytes(source=lambda: 10) # function # Error 
Enter fullscreen mode Exit fullscreen mode
# Empty bytes v = bytes() v = bytes(b'') print(v) # b'' 
Enter fullscreen mode Exit fullscreen mode
v = bytes(source=b'12') # bytes v = bytes(source=bytearray(b'12')) # bytearray  print(v, v[0], v[1]) # b'12' 49 50 
Enter fullscreen mode Exit fullscreen mode
v = bytes(source=12) # int  print(v, v[0], v[1]) # b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' 0 0 
Enter fullscreen mode Exit fullscreen mode
v = bytes(source=[1, 2, 3]) # list v = bytes(source=(1, 2, 3)) # tuple  print(v, v[0], v[1], v[2]) # b'\x01\x02\x03' 1 2 3 
Enter fullscreen mode Exit fullscreen mode
v = bytes(source={1, 2, 3}) # set v = bytes(source=frozenset({1, 2, 3})) # frozenset(int)  print(v, v[0], v[1], v[2]) # b'\x01\x02\x03' 1 2 3 
Enter fullscreen mode Exit fullscreen mode
v = bytes(source={1:2, 3:4}) # dict(int:int) v = bytes(source={1:2, 3:4}.keys()) # dict(int:int).keys()  print(v, v[0], v[1]) # b'\x01\x03' 1 3 
Enter fullscreen mode Exit fullscreen mode
v = bytes(source={1:2, 3:4}.values()) # dict(int:int).values()  print(v, v[0], v[1]) # b'\x02\x04' 2 4 
Enter fullscreen mode Exit fullscreen mode
v = bytes(source=iter([1, 2, 3])) # iterator  print(v, v[0], v[1], v[2]) # b'\x01\x02\x03' 1 2 3 
Enter fullscreen mode Exit fullscreen mode
v = bytes(source=range(1, 4)) # range  print(v, v[0], v[1], v[2]) # b'\x01\x02\x03' 1 2 3 
Enter fullscreen mode Exit fullscreen mode

<Encode a string to an immutable byte string(bytes)>:

v = "Lёт's gφ!" # Let's go!  print(bytes(source=v, encoding='utf-8')) print(bytes(source=v, encoding='utf-8', errors='strict')) # b"L\xd1\x91\xd1\x82's g\xcf\x86!" 
Enter fullscreen mode Exit fullscreen mode
v = "Lёт's gφ!" # Let's go!  print(bytes(source=v, encoding='utf-7')) # b"L+BFEEQg's g+A8Y!" 
Enter fullscreen mode Exit fullscreen mode
v = "Lёт's gφ!" # Let's go!  print(bytes(source=v, encoding='utf-16')) # b"\xff\xfeL\x00Q\x04B\x04'\x00s\x00 \x00g\x00\xc6\x03!\x00" 
Enter fullscreen mode Exit fullscreen mode
v = "Lёт's gφ!" # Let's go!  print(bytes(source=v, encoding='big5')) # b"L\xc7\xce\xc7\xdb's g\xa3p!" 
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!  print(bytes(source=v, encoding='ascii', errors='ignore')) # b"L's g!"  print(bytes(source=v, encoding='ascii', errors='replace')) # b"L??'s g?!"  print(bytes(source=v, encoding='ascii', errors='xmlcharrefreplace')) # b"L&#1105;&#1090;'s g&#966;!"  print(bytes(source=v, encoding='ascii', errors='backslashreplace')) # b"L\\u0451\\u0442's g\\u03c6!"  print(bytes(source=v, encoding='ascii', errors='hashreplace')) # b"L##'s g#!"  print(bytes(source=v, 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

Top comments (0)