DEV Community

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

Posted on • Edited on

Bytes in Python (4)

Buy Me a Coffee

*Memo:

A bytes can be read by indexing as shown below:

*Memo:

  • Indexing can be done with one or more [index] in the range [The 1st index, The last index]:
    • index(Required-Type:int):
      • Don't use index=.
    • index can be signed indices(zero and positive and negative indices).
    • Error occurs if index is out of range.
    • [slice][index] can be used.
v = b'ABCDEFGH' print(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7]) print(v[-8], v[-7], v[-6], v[-5], v[-4], v[-3], v[-2], v[-1]) print(v[0:1][0], v[1:2][0], v[2:3][0], v[3:4][0], v[4:5][0], v[5:6][0], v[6:7][0], v[7:8][0]) print(v[-8:-7][0], v[-7:-6][0], v[-6:-5][0], v[-5:-4][0], v[-4:-3][0], v[-3:-2][0], v[-2:-1][0], v[-1:8][0]) # 65 66 67 68 69 70 71 72 
Enter fullscreen mode Exit fullscreen mode

A bytes can be read by slicing as shown below:

*Memo:

  • Slicing can be done with one or more [start:end:step] in the range [start, end):
    • start(Optional-Default:None-Type:int/NoneType):
      • It's a start index(inclusive).
      • If it's None, it's the 1st index.
      • Don't use start=.
    • end(Optional-Default:None-Type:int/NoneType):
      • It's an end index(exclusive).
      • If it's None, it's the bytes length.
      • Don't use end=.
    • step(Optional-Default:None-Type:int/NoneType):
      • It's the interval of indices.
      • If it's None, it's 1.
      • It cannot be zero.
      • Don't use end=.
    • The [] with at least one : is slicing.
    • start and end can be signed indices(zero and positive and negative indices).
    • Error doesn't occur even if [start, end) is out of the range [The 1st index, The bytes length).
v = 'ABCDEFGH' print(v[:]) print(v[::]) print(v[None:None:None]) print(v[0:8:1]) print(v[-8:8:1]) print(v[-100:100:1]) # b'ABCDEFGH'  print(v[::2]) # b'ACEG'  print(v[::-2]) # b'HFDB'  print(v[2:]) print(v[-6:]) print(v[2::]) print(v[-6::]) # b'CDEFGH'  print(v[2::2]) print(v[-6::2]) # b'CEG'  print(v[2::-2]) print(v[-6::-2]) # b'CA'  print(v[:6]) print(v[:-2]) print(v[:6:]) print(v[:-2:]) # b'ABCDEF'  print(v[:6:2]) print(v[:-2:2]) # b'ACE'  print(v[:6:-2]) print(v[:-2:-2]) # b'H'  print(v[2:6]) print(v[-6:-2]) print(v[2:6:]) print(v[-6:-2:]) # b'CDEF'  print(v[2:6:2]) print(v[-6:-2:2]) # b'CE'  print(v[2:6:-2]) print(v[-6:-2:-2]) # b'' 
Enter fullscreen mode Exit fullscreen mode
v = b'ABCDEFGH' print(v[-9], v[8]) # IndexError: index out of range 
Enter fullscreen mode Exit fullscreen mode

A bytes cannot be changed by indexing, slicing and a del statement as shown below:

*Memo:

  • A del statement cannot remove zero or more bytes from a bytes by indexing and slicing but can remove one or more variables themselves.
v = b'abcdef' v[1] = b'X' v[-5] = b'X' v[3:5] = [b'Y', b'Z'] v[-3:-1] = [b'Y', b'Z'] v[1], v[3:5] = b'X', [b'Y', b'Z'] v[-5], v[-3:-1] = b'X', [b'Y', b'Z'] # TypeError: 'bytes' object does not support item assignment 
Enter fullscreen mode Exit fullscreen mode
v = b'abcdef' del v[1], v[3:5] # del v[-5], v[-2:5] # TypeError: 'bytes' object does not support item deletion 
Enter fullscreen mode Exit fullscreen mode
v = b'abcdef' del v print(v) # NameError: name 'v' is not defined 
Enter fullscreen mode Exit fullscreen mode

If you really want to change a bytes, use bytearray(), ord() and bytes() as shown below.

v = b'abcdef' v = bytearray(v) v[1] = ord(b'X') v[-5] = ord(b'X') v[3:5] = [ord(b'Y'), ord(b'Z')] v[-3:-1] = [ord(b'Y'), ord(b'Z')] v[3:5] = b'YZ' v[-3:-1] = b'YZ' v[3:5] = bytearray(b'YZ') v[-3:-1] = bytearray(b'YZ') v[1], v[3:5] = ord(b'X'), [ord(b'Y'), ord(b'Z')] v[-5], v[-3:-1] = ord(b'X'), [ord(b'Y'), ord(b'Z')] v[1], v[3:5] = ord(b'X'), b'YZ' v[-5], v[-3:-1] = ord(b'X'), b'YZ' v[1], v[3:5] = ord(b'X'), bytearray(b'YZ') v[-5], v[-3:-1] = ord(b'X'), bytearray(b'YZ') v = bytes(v) print(v) # b'aXcYZf 
Enter fullscreen mode Exit fullscreen mode
v = b'abcdef' v = bytearray(v) del v[1], v[3:5] # del v[-5], v[-2:5]  v = bytes(v) print(v) # b'acd' 
Enter fullscreen mode Exit fullscreen mode

In addition, if you really want to change a bytes, use list(), chr(), str.encode() or int.to_bytes() and bytes.join() as shown below.

v = b'abcdef' v = [chr(x).encode() for x in list(v)] # v = [x.to_bytes() for x in v]  v[1] = b'X' v[-5] = b'X' v[3:5] = [b'Y', b'Z'] v[-3:-1] = [b'Y', b'Z'] v[1], v[3:5] = b'X', [b'Y', b'Z'] v[-5], v[-3:-1] = b'X', [b'Y', b'Z'] v = b''.join(v) print(v) # b'aXcYZf' 
Enter fullscreen mode Exit fullscreen mode
v = b'abcdef' v = [chr(x).encode() for x in list(v)] # v = [x.to_bytes() for x in v]  del v[1], v[3:5] # del v[-5], v[-2:5]  v = b''.join(v) print(v) # b'acd' 
Enter fullscreen mode Exit fullscreen mode

A bytes can be continuously used through multiple variables as shown below:

v1 = v2 = v3 = b'ABCDE' # Equivalent  # v1 = b'ABCDE' print(v1) # b'ABCDE' # v2 = v1 print(v2) # b'ABCDE' # v3 = v2 print(v3) # b'ABCDE' 
Enter fullscreen mode Exit fullscreen mode

A bytes cannot be shallow-copied and deep-copied as shown below:

<Shallow & Deep copy>:

*Memo:

  • v1 and v2 refer to the same bytes.
  • is can check if v1 and v2 refer to the same bytes and each same byte.
  • copy.copy(), bytes() and slicing cannot shallow-copy a bytes.
  • copy.deepcopy() cannot deep-copy and even shallow-copy a bytes.
import copy v1 = b'ABCDE' v2 = copy.copy(v1) v2 = bytes(v1) v2 = v1[:] v2 = copy.deepcopy(v1) print(v1, v1[2]) # b'ABCDE' 67 print(v2, v2[2]) # b'ABCDE' 67  print(v1 is v2, v1[2] is v2[2]) # True True 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)