DEV Community

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

Posted on • Edited on

Bytes in Python (1)

Buy Me a Coffee

*Memo:

A bytes:

  • is the ordered collection of zero or more bytes represented with ASCII characters and whose type is bytes for computer to understand:
    • Each byte must be between [0, 255] from 256 ASCII characters.
    • It's for computer to understand.
    • It's also called a byte string.
    • Ordered means that the order of each byte in a bytes is kept so it guarantees that the order is always the same.
  • cannot be huge because it gets OverflowError.
  • is immutable(hashable) so it cannot be changed.
  • can be created by the bytes literal b or B with '', "", '''''' or """""" or 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.
  • can be iterated with a for statement.
  • can be unpacked with an assignment and for statement, function and * but not with **.
  • is False if they're empty.
  • can be checked if a specific element is in the bytes with in keyword.
  • can be checked if the bytes is referred to by two variables with is keyword.
  • can be enlarged with * and a number.
  • can be used with len() to get the length.
  • can be read but cannot be changed by indexing or slicing.
  • can be continuously used through multiple variables.
  • cannot be shallow-copied and deep-copied.

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

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'])
v = b'ABCDE' * 3
v = b'01234' * 3
v = b'' * 3
# No error

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

A bytes is the ordered collection of zero or more bytes represented with ASCII characters and whose type is bytes as shown below:

v = b'' # Empty bytes

print(v)
# b''

print(type(v))
# <class 'bytes'>
Enter fullscreen mode Exit fullscreen mode
v = b"Hello World"

print(v)
# b'Hello World'
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 iterated with a for statement as shown below:

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

A bytes can be unpacked with an assignment and for statement, function and * but not with ** as shown below:

v1, v2, v3 = b'ABC'

print(v1, v2, v3)
# 65 66 67
Enter fullscreen mode Exit fullscreen mode
v1, *v2, v3 = b'ABCDEF'

print(v1, v2, v3)  # 65 [66, 67, 68, 69] 70
print(v1, *v2, v3) # 65 66 67 68 69 70
Enter fullscreen mode Exit fullscreen mode
for v1, v2, v3 in [b'ABC', b'DEF']:
    print(v1, v2, v3)
# 65 66 67
# 68 69 70
Enter fullscreen mode Exit fullscreen mode
for v1, *v2, v3 in [b'ABCDEF', b'GHIJKL']:
    print(v1, v2, v3)
    print(v1, *v2, v3)
# 65 [66, 67, 68, 69] 70
# 65 66 67 68 69 70
# 71 [72, 73, 74, 75] 76
# 71 72 73 74 75 76
Enter fullscreen mode Exit fullscreen mode
print(*b'ABCD', *b'EF')
# 65 66 67 68 69 70
Enter fullscreen mode Exit fullscreen mode
print([*b'ABCD', *b'EF'])
# [65, 66, 67, 68, 69, 70]
Enter fullscreen mode Exit fullscreen mode
def func(p1='a', p2='b', p3='c', p4='d', p5='e', p6='f'):
    print(p1, p2, p3, p4, p5, p6)

func()
# a b c d e f

func(*b'ABCD', *b'EF')
# 65 66 67 68 69 70
Enter fullscreen mode Exit fullscreen mode
def func(p1='a', p2='b', *args):
    print(p1, p2, args)
    print(p1, p2, *args)
    print(p1, p2, [0, 1, *args, 2, 3])

func()
# a b ()
# a b Nothing
# a b [0, 1, 2, 3]

func(*b'ABCD', *b'EF')
# 65 66 (67, 68, 69, 70)
# 65 66 67 68 69 70
# 65 66 [0, 1, 67, 68, 69, 70, 2, 3]
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 can be checked if a specific element is in the bytes with in keyword as shown below:

v = b'ABCD'

print(b'B' in v)
# True

print(b'CD' in v)
# True

print(b'b' in v)
# False
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

Be careful, a huge bytes gets I/O error as shown below:

v = b'ABCDE' * 1000000000

print(v)
# OverflowError: repeated bytes are too long
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

Top comments (0)