Byte string 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

Byte string in Python (1)

Publish Date: Jul 23
0 0

Buy Me a Coffee

*Memo for a byte string(bytes and bytearray) and string:

*Memo for string and byte string(bytes and bytearray) functions:

*Memo for byte string(bytearray) functions:

*Memo for others:

  • My post explains a list and the list with indexing.
  • My post explains a tuple.
  • My post explains a set and the set with copy.
  • My post explains a dictionary (1).
  • My post explains an iterator (1).

A byte string:

  • is the sequence of zero or more bytes(0~255 integers) represented with characters whose type is bytes or bytearray for computer to understand:
  • shouldn't be huge not to get I/O error.
  • of a bytes literal or bytes() is immutable so it cannot be changed.
  • of bytearray() is mutable so it can be changed.
  • can be created by the bytes literal b or B with '', "", '''''' or """""" or bytes() or bytearray() with or without several types of values:
    • A bytes literal can only have 256 ASCII characters.
    • A bytes literal cannot be used for a docstring even though it gets None with no error.
    • b or B with '' or "" are for one line.
    • b or B with '''''' or """""" are for one or more lines.
    • For bytes() and bytearray(), 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 enlarged with * and a number.
  • of a bytes literal or bytes() can be read but cannot be changed by indexing or slicing.
  • of bytearray() can be read or 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 the sequence of zero or more characters whose type is bytes or bytearray for computer to understand as shown below. *\' is the escape sequence to output ':

v = b'' # Empty string
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 
"""

print(type(b'Hello World'))
print(type(bytes(b'Hello World')))
# <class 'bytes'>

print(type(bytearray(b'Hello World')))
# <class 'bytearray'>

v = b"Lёт's gφ!" # Let's go!
# SyntaxError: bytes can only contain ASCII literal characters
Enter fullscreen mode Exit fullscreen mode
v = b'' # Empty string

print(v)
# b''
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 byte string can be enlarged with * and a number as shown below:

v = b'ABC' * 3
v = bytes(b'ABC') * 3

print(v)
# b'ABCABCABC'
Enter fullscreen mode Exit fullscreen mode
v = bytearray(b'ABC') * 3

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

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

v = b'ABC' * 100000000
v = bytes(b'ABC') * 100000000

print(v)
# OSError: [Errno 29] I/O error
Enter fullscreen mode Exit fullscreen mode
v = bytearray(b'ABC') * 100000000

print(v)
# OSError: [Errno 29] I/O error
Enter fullscreen mode Exit fullscreen mode

Comments 0 total

    Add comment