Byte string in Python (3)
Super Kai (Kazuya Ito)

Super Kai (Kazuya Ito) @hyperkai

About: I'm a web developer. Buy Me a Coffee: ko-fi.com/superkai SO: stackoverflow.com/users/3247006/super-kai-kazuya-ito X(Twitter): twitter.com/superkai_kazuya FB: facebook.com/superkai.kazuya

Joined:
Oct 21, 2021

Byte string in Python (3)

Publish Date: Aug 21
0 0

Buy Me a Coffee

*Memos:

The byte string of bytearray() can be read by indexing or slicing as shown below:

*Memos:

  • Indexing can be done with one or more [index].
  • Slicing can be done with one or more [start:end:step]:
    • start(Optional-Default:The index of the 1st element).
    • end(Optional-Default:The index of the last element + 1).
    • step(Optional-Default:1). *step cannot be zero.
    • The [] with at least one : is slicing.
v = bytearray(b'ABCDEFGH')

print(v)
# bytearray(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])
# 65 66 67 68 69 70 71 72
Enter fullscreen mode Exit fullscreen mode
v = bytearray(b'ABCDEFGH')

print(v[:])
print(v[::])
# bytearray(b'ABCDEFGH')

print(v[::2])
# bytearray(b'ACEG')

print(v[::-2])
# bytearray(b'HFDB')

print(v[2:])
print(v[-6:])
print(v[2::])
print(v[-6::])
# bytearray(b'CDEFGH')

print(v[2::2])
print(v[-6::2])
# bytearray(b'CEG')

print(v[2::-2])
print(v[-6::-2])
# bytearray(b'CA')

print(v[:6])
print(v[:-2])
print(v[:6:])
print(v[:-2:])
# bytearray(b'ABCDEF')

print(v[:6:2])
print(v[:-2:2])
# bytearray(b'ACE')

print(v[:6:-2])
print(v[:-2:-2])
# bytearray(b'H')

print(v[2:6])
print(v[-6:-2])
print(v[2:6:])
print(v[-6:-2:])
# bytearray(b'CDEF')

print(v[2:6:2])
print(v[-6:-2:2])
# bytearray(b'CE')

print(v[2:6:-2])
print(v[-6:-2:-2])
# bytearray(b'')
Enter fullscreen mode Exit fullscreen mode

The byte string of bytearray() can be changed by indexing or slicing as shown below:

*Memos:

  • An iterable must be assigned to a sliced variable.
  • A del statement can be used to remove one or more bytes from a list by indexing or slicing and can remove one or more variables themselves.
v = bytearray(b'abcdef')

v[0] = ord(b'X')
v[2:6] = [ord(b'Y'), ord(b'Z')]

print(v)
# bytearray(b'XbYZ')

print(bytes(v))
# b'XbYZ'
Enter fullscreen mode Exit fullscreen mode
v = bytearray(b'abcdef')

del v[0], v[3:5]

print(v)
# bytearray(b'bcd')

print(bytes(v))
# b'bcd'
Enter fullscreen mode Exit fullscreen mode
v = bytearray(b'abcdef')

del v

print(v)
# NameError: name 'v' is not defined
Enter fullscreen mode Exit fullscreen mode

The variables v1 and v2 refer to the same byte string of bytearray() unless copied as shown below:

*Memos:

  • is keyword can check if v1 and v2 refer to the same byte string.
  • bytearray().copy(), copy.copy() and slicing do shallow copy. *bytearray().copy() has no arguments.
  • copy.deepcopy() does deep copy.
  • copy.deepcopy() should be used because it's safe, doing copy deeply while bytearray().copy(), copy.copy() and slicing aren't safe, doing copy shallowly.
import copy

v1 = bytearray(b'abcde')

v2 = v1 # v2 refers to the same byte string as v1.

v2[2] = ord('X') # Changes the same byte string as v1.
v2[2] = 88      
                        # ↓
print(v1) # bytearray(b'abXde')
print(v2) # bytearray(b'abXde') 
                        # ↑
print(v1 is v2) # True

v2 = v1.copy() # v2 refers to the different byte string from v1.
v2 = copy.copy(v1)
v2 = copy.deepcopy(v1)
v2 = v1[:]

v2[2] = ord('Y') # Changes the different byte string from v1.
v2[2] = 89
                        # ↓
print(v1) # bytearray(b'abXde')
print(v2) # bytearray(b'abYde')
                        # ↑
print(v1 is v2) # False
Enter fullscreen mode Exit fullscreen mode

Comments 0 total

    Add comment