*Memo:
A bytes can be read by indexing and slicing as shown below:
*Memo:
- Indexing can be done with one or more
[index]
in the range[The 1st byte index, The last byte index]
:-
index
(Required-Type:int
):- Don't use
index=
.
- Don't use
- Error occurs if
index
is out of range.
-
- 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 byte 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 next index to the last byte index. - Don't use
end=
.
-
step
(Optional-Default:None
-Type:int
/NoneType):- It's the interval of indices.
- If it's
None
, it's1
. - It cannot be zero.
- Don't use
end=
.
- The
[]
with at least one:
is slicing. - Error doesn't occur even if
[start, end)
is out of the range[The 1st byte index, The last byte index]
.
-
-
index
,start
andend
can be signed indices(zero and positive and negative indices).
v = b'ABCDEFGH'
print(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])
# 65 66 67 68 69 70 71 72
print(v[:])
print(v[::])
print(v[None:None:None])
print(v[0: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''
v = b'ABCDEFGH'
print(v[-9], v[8])
# IndexError: index out of range
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
v = b'abcdef'
del v[1], v[3:5]
# del v[-5], v[-2:5]
# TypeError: 'bytes' object does not support item deletion
v = b'abcdef'
del v
print(v)
# NameError: name 'v' is not defined
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
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'
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'
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'
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'
A bytes cannot be shallow-copied and deep-copied as shown below:
<Shallow & Deep copy>:
*Memo:
-
v1
andv2
refer to the same bytes. -
is
keyword can check ifv1
andv2
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
Top comments (0)