This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)
*Memos:
- My post explains a string.
A byte string:
- is a sequence of a zero or more characters whose type is
bytes
. - is immutable so it cannot be changed.
- can be created with
b
orB
with''
or""
or bytes(). - can be enlarged with
*
and a number. - can be accessed but cannot be changed by indexing or slicing.
Be careful, a huge byte string gets I/O error
.
b
or B
with ''
or ""
can create the byte string which is a sequence of zero or more characters whose type is bytes
or bytearray
as shown below. *\'
is the escape sequence to output '
:
v = b'Hello World'
v = b"Hello World"
v = B'Hello World'
v = B"Hello World"
v = b'I\'m John.'
v = B"I'm John."
v = b'Hello' B" World"
v = b'' # Empty string
# No error
print(type(b'Hello World'))
print(type(bytes(b'Hello World')))
# <class 'bytes'>
print(type(bytearray(b'Hello World')))
# <class 'bytearray'>
v = b'Hello World'
v = b"Hello World"
v = B'Hello World'
v = B"Hello World"
v = b'Hello' B" World"
print(v)
# b'Hello World'
v = b'I\'m John.'
v = B"I'm John."
print(v)
# b"I'm John."
v = b'' # Empty string
print(v)
# b''
A byte string can be enlarged with *
and a number as shown below:
v = b'abc' * 3
print(v)
# b'abcabcabc'
Be careful, a huge byte string gets I/O error
as shown below:
v = b'abc' * 100000000
print(v)
# line 3, in OSError: [Errno 29] I/O error
You can access a byte string by indexing or slicing as shown below:
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])
# 97 98 99 100 101 102 103 104
v = b'abcdefgh'
print(v[:])
print(v[::])
# 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''
You cannot change a byte string because it’s immutable as shown below. *A del statement can still be used to remove a variable itself:
v = b'abcdef'
v[0] = b'X'
# v[-6] = b'X'
v[2:6] = [b'Y', b'Z']
# TypeError: 'bytes' object does not support item assignment
v = b'abcdef'
del v[0]
# del v[-6]
del v[3: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 byte string, use list(), chr(), str.encode() and bytes.join() as shown below.
v = b'abcdef'
v = [chr(x).encode() for x in list(v)]
v[0] = b'X'
# v[-6] = b'X'
v[2:6] = [b'Y', b'Z']
v = b''.join(v)
print(v)
# b'XbYZ'
v = b'abcdef'
v = [chr(x).encode() for x in list(v)]
del v[0]
# del v[-6]
del v[3:5]
v = b''.join(v)
print(v)
# b'bcd'
In addition, if you really want to change a byte string, use bytearray()
, ord() and bytes()
as shown below.
v = b'abcdef'
v = bytearray(v)
v[0] = ord(b'X')
# v[-6] = ord(b'X')
v[2:6] = [ord(b'Y'), ord(b'Z')]
v = bytes(v)
print(v)
# b'XbYZ'
v = b'abcdef'
v = bytearray(v)
del v[0]
# del v[-6]
del v[3:5]
v = bytes(v)
print(v)
# b'bcd'
This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)