Bytes in Python (1)
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

Bytes in Python (1)

Publish Date: Jul 23
0 0

Buy Me a Coffee

*Memo:

A bytes (object):

  • is the ordered immutable(hashable) collection of the zero or more bytes represented with ASCII characters whose type is bytes and which allows duplicated bytes:
    • Each byte must be between [0, 255] from 256 ASCII characters.
    • A bytes is for computer to understand.
    • A bytes is also called a byte string.
    • Ordered means that the order of each byte in a bytes object is kept so it guarantees that the order is always the same.
    • Immutable(Hashable) means the bytes of a bytes object cannot be changed.
  • can be used with len() to get the length.
  • is False if it's empty.
  • can be checked if zero or more specific bytes are in the bytes object with in keyword.
  • can be checked if the bytes object is referred to by two variables with is keyword.
  • and one or more bytes objects can be concatenated with + but not with |.
  • can be enlarged with * and a number.
  • can be iterated with a for statement.
  • can be created by the bytes literal b or B with '', "", '''''' or """""" and by bytes() with or without several types of values:
    • A bytes literal cannot be used for a docstring because it gets None.
    • b or B with '' or "" are for one line.
    • b or B with '''''' or """""" are for one or more lines.
    • For bytes(), the words type conversion are also suitable in addition to the word creation.
  • can be encoded to from a string by encode():
    • For encode(), the words creation and type conversion are also suitable in addition to the word encoding.
  • cannot be big because it gets OverflowError.
  • can be read by indexing and slicing.
  • cannot be changed by indexing, slicing and a del statement.
  • can be unpacked with an assignment and for statement, function and * but not with **.
  • can be continuously used through multiple variables.
  • cannot be shallow-copied by copy.copy(), bytes() and slicing.
  • cannot be deep-copied and even shallow-copied by copy.deepcopy().

Be careful, a big bytes gets OverflowError.


b or B with '', "", '''''' or """""" can create a bytes as shown below:

*Memo:

  • \' is the escape sequence to output '.
v = b'' # Empty bytes
v = b"Hello World"
v = B'I\'m John.'
v = B"I'm John."
v = b'''I'm John.'''
v = b"""I'm John."""
v = B'''Apple Orange Banana Kiwi'''
v = b'Apple' b" Orange" b'''Banana''' B"""Kiwi"""
v = b'''Apple
Orange
Banana
Kiwi'''
v = b"""
Apple
   Orange
       Banana
           Kiwi
"""
b'These above get no error'
b"These above get no error"
b'''These above get no error'''
b"""These above get no error"""
b''' 
These above 
get no error 
'''
b"""
These above 
get no error 
"""
# No error

print(len(b"Let's go!"))
v = b'ABC' + b'DE' + b'FGHI'
v = b'ABCDE' * 3
v = b'01234' * 3
v = b'' * 3
for v in b'ABC': pass
v1, v2, v3 = b'ABC'
v1, *v2, v3 = b'ABCDEF'
for v1, v2, v3 in [b'ABC', b'DEF']: pass
for v1, *v2, v3 in [b'ABCDEF', b'GHIJKL']: pass
print(*b'ABCD', *b'EF')
print([*b'ABCD', *b'EF'])
# No error

v = b"Lёт's gφ!" # Let's go!
v = b'ABC' | b'DE' | b'FGHI'
v = b'ABCDE' * 1000000000
# Error
Enter fullscreen mode Exit fullscreen mode

A bytes object is the ordered immutable(hashable) collection of the zero or more bytes represented with ASCII characters whose type is bytes and which allows duplicated bytes as shown below:

v = b"Hello World"

print(v)
# b'Hello World'

print(type(v))
# <class 'bytes'>

v[1] = b'X'
v[3] = b'Y'
# TypeError: 'bytes' object does not support item assignment
Enter fullscreen mode Exit fullscreen mode
v = b'' # Empty bytes

print(v)
# b''
Enter fullscreen mode Exit fullscreen mode
v = b"Lёт's gφ!" # Let's go!
# SyntaxError: bytes can only contain ASCII literal characters
Enter fullscreen mode Exit fullscreen mode
v = B"I'm John."
v = B'I\'m John.'
v = b'''I'm John.'''
v = b"""I'm John."""

print(v)
# b"I'm John."
Enter fullscreen mode Exit fullscreen mode
v = B'''Apple Orange Banana Kiwi'''
v = b'Apple' b" Orange" b''' Banana''' B""" Kiwi"""

print(v)
# b'Apple Orange Banana Kiwi'
Enter fullscreen mode Exit fullscreen mode
v = b'''Apple
Orange
Banana
Kiwi'''

print(v)
# b'Apple\nOrange\nBanana\nKiwi'
Enter fullscreen mode Exit fullscreen mode
v = b"""
Apple
   Orange
       Banana
           Kiwi
"""

print(v)
# b'\nApple\n   Orange\n       Banana\n           Kiwi\n'
Enter fullscreen mode Exit fullscreen mode

A bytes can be used with len() to get the length as shown below:

v = b"Let's go!"

print(len(v))
# 9
Enter fullscreen mode Exit fullscreen mode

An empty bytes is False as shown below:

print(bool(b''))  # Empty bytes
# False

print(bool(b' ')) # bytes
# True
Enter fullscreen mode Exit fullscreen mode

A bytes object can be checked if zero or more specific bytes are in the bytes object with in keyword as shown below:

v = b'ABC'

print(b'' in v)
print(b'A' in v)
print(b'BC' in v)
print(b'ABC' in v)
# True

print(b'a' in v)
# False
Enter fullscreen mode Exit fullscreen mode

A bytes object and one or more bytes objects can be concatenated with + but not with | as shown below:

v = b'ABC' + b'DE' + b'FGHI'

print(v)
# b'ABCDEFGHI'
Enter fullscreen mode Exit fullscreen mode
v = b'ABC' | b'DE' | b'FGHI'
# TypeError: unsupported operand type(s) for |: 'bytes' and 'bytes'
Enter fullscreen mode Exit fullscreen mode

A bytes can be enlarged with * and a number as shown below:

v = b'ABCDE' * 3

print(v)
# b'ABCDEABCDEABCDE'
Enter fullscreen mode Exit fullscreen mode
v = b'01234' * 3

print(v)
# b'012340123401234'
Enter fullscreen mode Exit fullscreen mode
v = b'' * 3

print(v)
# b''
Enter fullscreen mode Exit fullscreen mode

A bytes can be iterated with a for statement as shown below:

for v in b'ABC':
    print(v)
# 65
# 66
# 67
Enter fullscreen mode Exit fullscreen mode

Be careful, a big bytes gets OverflowError as shown below:

v = b'ABCDE' * 1000000000

print(v)
# OverflowError: repeated bytes are too long
Enter fullscreen mode Exit fullscreen mode

Comments 0 total

    Add comment