*Memo for a bytes, bytearray and string:
- My post explains the bytearray with indexing, slicing and copy.
- My post explains bytearray().
- My post explains a bytes.
- My post explains a string.
*Memo for string, bytes and bytearray functions:
- My post explains encode() and decode().
- My post explains upper(), lower() and casefold().
- My post explains swapcase(), title() and capitalize().
- My post explains isupper(), islower() and istitle().
- My post explains count().
- My post explains index().
- My post explains rindex().
- My post explains find().
- My post explains rfind().
- My post explains split().
- My post explains rsplit().
- My post explains splitlines().
- My post explains partition().
- My post explains rpartition().
- My post explains join().
- My post explains replace().
- My post explains removeprefix() and removesuffix().
- My post explains startswith().
- My post explains endswith().
- My post explains center().
- My post explains ljust().
- My post explains rjust().
- My post explains zfill().
- My post explains expandtabs().
- My post explains strip().
- My post explains lstrip().
- My post explains rstrip().
- My post explains maketrans().
- My post explains translate().
- My post explains isdecimal(), isdigit() and isnumeric().
- My post explains isalpha() and isalnum().
- My post explains isascii(), isspace(), isprintable() and isidentifier().
- My post explains iskeyword() and issoftkeyword().
- My post explains ord(), sorted() and reversed().
*Memo for 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 bytearray:
- is the sequence of zero or more bytes represented with ASCII characters and whose type is
bytearray
:- Each byte must be
[0, 255]
from 256 ASCII characters. - It's for computer to understand.
- Each byte must be
- shouldn't be huge not to get
I/O error
. - is mutable so it can be changed.
- can be created by
bytearray()
with or without several types of values:- For
bytearray()
, the words type conversion are also suitable in addition to the word creation.
- For
- 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.
- For
- can be iterated with a
for
statement. - can be unpacked with an assignment and
for
statement, function and*
but not with**
. - is
False
if it's empty. - can be checked if a specific element is or isn't in it with
in
keyword ornot
andin
keyword respectively. - can be checked if it is or isn't referred to by two variables with
is
keyword ornot
andis
keyword respectively. - can be enlarged with
*
and a number. - can be read or changed by indexing or slicing.
Be careful, a huge byte string gets I/O error
.
bytearray()
can create a bytearray as shown below:
*Memo:
-
\'
is the escape sequence to output'
.
v = bytearray(b'') # Empty bytearray
v = bytearray(b"Hello World")
v = bytearray(B'I\'m John.')
v = bytearray(B"I'm John.")
v = bytearray(b'''I'm John.''')
v = bytearray(b"""I'm John.""")
v = bytearray(B'''Apple Orange Banana Kiwi''')
v = bytearray(b'Apple' b" Orange" b'''Banana''' B"""Kiwi""")
v = bytearray(b'''Apple
Orange
Banana
Kiwi''')
v = bytearray(b"""
Apple
Orange
Banana
Kiwi
""")
# No error
for v in bytearray(b'ABC'): pass
v1, v2, v3 = bytearray(b'ABC')
v1, *v2, v3 = bytearray(b'ABCDEF')
for v1, v2, v3 in [bytearray(b'ABC'), bytearray(b'DEF')]: pass
for v1, *v2, v3 in [bytearray(b'ABCDEF'), bytearray(b'GHIJKL')]: pass
print([*bytearray(b'ABC'), *bytearray(b'DE')])
print(*bytearray(b'ABC'), *bytearray(b'DE'))
v = bytearray(b'ABC') * 3
v = bytearray(b'012') * 3
v = bytearray(b'') * 3
# No error
v = b"Lёт's gφ!" # Let's go!
print(**bytearray(b'ABCDE'))
v = bytearray(b'ABC') * 100000000
# Error
A bytearray is the sequence of zero or more bytes represented with ASCII characters and whose type is bytearray
as shown below:
v = bytearray(b'') # Empty bytearray
print(v)
# bytearray(b'')
v = bytearray(b"Hello World")
print(v)
# bytearray(b'Hello World')
v = bytearray(B"I'm John.")
v = bytearray(B'I\'m John.')
v = bytearray(b'''I'm John.''')
v = bytearray(b"""I'm John.""")
print(v)
# bytearray(b"I\'m John.")
v = bytearray(B'''Apple Orange Banana Kiwi''')
v = bytearray(b'Apple' b" Orange" b''' Banana''' B""" Kiwi""")
print(v)
# bytearray(b'Apple Orange Banana Kiwi')
v = bytearray(b'''Apple
Orange
Banana
Kiwi''')
print(v)
# bytearray(b'Apple\nOrange\nBanana\nKiwi')
v = bytearray(b"""
Apple
Orange
Banana
Kiwi
""")
print(v)
# bytearray(b'\nApple\n Orange\n Banana\n Kiwi\n')
A bytearray can be iterated with a for
statement as shown below:
for v in bytearray(b'ABC'):
print(v)
# 65
# 66
# 67
A bytearray can be unpacked with an assignment and for
statement, function and *
but not with **
as shown below:
v1, v2, v3 = bytearray(b'ABC')
print(v1, v2, v3)
# 65 66 67
v1, *v2, v3 = bytearray(b'ABCDEF')
print(v1, v2, v3) # 65 [66, 67, 68, 69] 70
print(v1, *v2, v3) # 65 66 67 68 69 70
for v1, v2, v3 in [bytearray(b'ABC'), bytearray(b'DEF')]:
print(v1, v2, v3)
# 65 66 67
# 68 69 70
for v1, *v2, v3 in [bytearray(b'ABCDEF'), bytearray(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
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(*bytearray(b'ABCD'), *bytearray(b'EF'))
# 65 66 67 68 69 70
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
# a b [0, 1, 2, 3]
func(*bytearray(b'ABCD'), *bytearray(b'EF'))
# 65 66 (67, 68, 69, 70)
# 65 66 67 68 69 70
# 65 66 [0, 1, 67, 68, 69, 70, 2, 3]
print([*bytearray(b'ABC'), *bytearray(b'DE')])
# [65, 66, 67, 68, 69]
print(*bytearray(b'ABC'), *bytearray(b'DE'))
# 65 66 67 68 69
print(**bytearray(b'ABCDE'))
# TypeError: print() argument after ** must be a mapping, not bytearray
An empty bytearray is False
as shown below:
print(bool(bytearray(b''))) # Empty bytearray
# False
print(bool(bytearray(b' '))) # bytearray
# True
A bytearray can be checked if a specific element is or isn't in it with in
keyword or not
and in
keyword respectively as shown below:
v = bytearray(b'ABCD')
print(bytearray(b'B') in v)
# True
print(bytearray(b'CD') in v)
# True
print(bytearray(b'b') in v)
# False
v = bytearray(b'ABCD')
print(bytearray(b'B') not in v)
# False
print(bytearray(b'CD') not in v)
# False
print(bytearray(b'b') not in v)
# True
A bytearray can be enlarged with *
and a number as shown below:
v = bytearray(b'ABC') * 3
print(v)
# bytearray(b'ABCABCABC')
v = bytearray(b'012') * 3
print(v)
# bytearray(b'012012012')
v = bytearray(b'') * 3
print(v)
# bytearray(b'')
Be careful, a huge bytearray gets I/O error
as shown below:
v = bytearray(b'ABC') * 100000000
print(v)
# OSError: [Errno 29] I/O error
Top comments (0)