DEV Community

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

Posted on • Edited on

Byte string in Python (3)

Buy Me a Coffee

*Memos:

bytearray() can create the mutable byte string whose type is bytearray or can encode a string as shown below:

*Memos:

  • The 1st argument is source(Optional-Default:b''-Type:bytes-like object/int/iterable(int) or Required-Type:str): *Memos:
    • It's optional with the default values b'' and bytes-like object/int/iterable(int) types if encoding or encoding and errors isn't/aren't set. *int gives a null value(\x00) which represents no value.
    • It's required with str to encode if encoding or encoding and errors is/are set, working as str.encode().
  • The 2nd argument is encoding(Optional-Default:'utf-8'): *Memos:
    • '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'): *Memos:
    • It controls decoding error with the error handlers, 'strict', 'ignore', 'replace', 'xmlcharrefreplace', 'backslashreplace', etc.
    • 'strict' raises UnicodeError if the character, which cannot be decoded, exists.
    • 'ignore' ignores the character which cannot be decoded.
    • 'replace' replaces the character, which cannot be decoded, with ?.
    • 'xmlcharrefreplace' replaces the character, which cannot be decoded, with a XML character e.g. ё.
    • 'backslashreplace' replaces the character, which cannot be decoded, with \\uxxxx e.g. \\u0451.
    • You can see more error handlers.
    • You can create your own error handler with codecs.register_error().
  • The created array of a byte string can use list functions e.g. append(), extend(), insert(), remove(), etc except clear().
v = bytearray() # Empty byte string v = bytearray(source=b'12') # Byte String v = bytearray(source=12) # Integer v = bytearray(source=[1, 2, 3]) # List(int) v = bytearray(source=(1, 2, 3)) # Tuple(int) v = bytearray(source={1, 2, 3}) # Set(int) v = bytearray(source={ord('a'):'A'}) # Dictionary(int:str) v = bytearray(source=iter([1, 2, 3])) # Iterator(int) # No error  print(type(bytearray(source=b'12'))) # <class 'bytearray'>  v = bytearray(source=1.2) # Floating-point number v = bytearray(source=1.2+3.4j) # Complex number v = bytearray(source=lambda: 10) # Function # Error 
Enter fullscreen mode Exit fullscreen mode
v = bytearray() # Empty byte string  print(v) # bytearray(b'') 
Enter fullscreen mode Exit fullscreen mode
v = bytearray(source=b'12') # Byte string  print(v, v[0], v[1]) # bytearray(b'12') 49 50 
Enter fullscreen mode Exit fullscreen mode
v = bytearray(source=12) # Integer  print(v, v[0], v[1]) # bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') 0 0 
Enter fullscreen mode Exit fullscreen mode
v = bytearray(source=[1, 2, 3]) # List  print(v, v[0], v[1], v[2]) # bytearray(b'\x01\x02\x03') 1 2 3 
Enter fullscreen mode Exit fullscreen mode
v = bytearray(source=(1, 2, 3)) # Tuple  print(v, v[0], v[1], v[2]) # bytearray(b'\x01\x02\x03') 1 2 3 
Enter fullscreen mode Exit fullscreen mode
v = bytearray(source={1, 2, 3}) # Set  print(v, v[0], v[1], v[2]) # bytearray(b'\x01\x02\x03') 1 2 3 
Enter fullscreen mode Exit fullscreen mode
v = bytearray(source={ord('a'):'A'}) # Dictionary  print(v, v[0]) # bytearray(b'a') 97 
Enter fullscreen mode Exit fullscreen mode
v = bytearray(source=iter([1, 2, 3])) # Iterator  print(v, v[0], v[1], v[2]) # bytearray(b'\x01\x02\x03') 1 2 3 
Enter fullscreen mode Exit fullscreen mode
v = "Lёт's gφ!" # Let's go!  print(bytearray(source=v, encoding='utf-8', errors='strict')) print(bytearray(source=v, encoding='utf-8')) # bytearray(b"L\xd1\x91\xd1\x82\'s g\xcf\x86!") 
Enter fullscreen mode Exit fullscreen mode
v = "Lёт's gφ!" # Let's go!  print(bytearray(source=v, encoding='utf-7')) # bytearray(b"L+BFEEQg\'s g+A8Y!") 
Enter fullscreen mode Exit fullscreen mode
v = "Lёт's gφ!" # Let's go!  print(bytearray(source=v, encoding='utf-16')) # bytearray(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(bytearray(source=v, encoding='big5')) # bytearray(b"L\xc7\xce\xc7\xdb\'s g\xa3p!") 
Enter fullscreen mode Exit fullscreen mode
import codecs def hashreplace_handler(s): return ((s.end - s.start) * '#', s.end) codecs.register_error('hashreplace', hashreplace_handler) v = "Lёт's gφ!" # Let's go!  print(bytearray(source=v, encoding='ascii', errors='ignore')) # bytearray(b"L\'s g!")  print(bytearray(source=v, encoding='ascii', errors='replace')) # bytearray(b"L??\'s g?!")  print(bytearray(source=v, encoding='ascii', errors='hashreplace')) # bytearray(b"L##\'s g#!")  print(bytearray(source=v, encoding='ascii')) print(bytearray(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
v = "Lёт's gφ!" # Let's go!  print(bytearray(source=v, encoding='ascii', errors='xmlcharrefreplace')) # bytearray(b"L&#1105;&#1090;\'s g&#966;!") 
Enter fullscreen mode Exit fullscreen mode
v = "Lёт's gφ!" # Let's go!  print(bytearray(source=v, encoding='ascii', errors='backslashreplace')) # bytearray(b"L\\u0451\\u0442\'s g\\u03c6!") 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)