*Memo for a byte string(bytes
and bytearray
) and string:
- My post explains bytes().
- My post explains bytearray().
-
My post explains a byte string(
bytes
) with indexing and slicing. -
My post explains a byte string(
bytearray
) with indexing, slicing and copy. - My post explains a string.
*Memo for string and byte 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 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
orbytearray
for computer to understand:- A bytes literal and bytes() are
bytes
. -
bytearray() is
bytearray
.
- A bytes literal and bytes() are
- 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
orB
with''
,""
,''''''
or""""""
orbytes()
orbytearray()
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
orB
with''
or""
are for one line. -
b
orB
with''''''
or""""""
are for one or more lines. - For
bytes()
andbytearray()
, 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.
- For
- 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
v = b'' # Empty string
print(v)
# b''
v = b"Hello World"
print(v)
# b'Hello World'
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."
v = B'''Apple Orange Banana Kiwi'''
v = b'Apple' b" Orange" b''' Banana''' B""" Kiwi"""
print(v)
# b'Apple Orange Banana Kiwi'
v = b'''Apple
Orange
Banana
Kiwi'''
print(v)
# b'Apple\nOrange\nBanana\nKiwi'
v = b"""
Apple
Orange
Banana
Kiwi
"""
print(v)
# b'\nApple\n Orange\n Banana\n Kiwi\n'
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'
v = bytearray(b'ABC') * 3
print(v)
# bytearray(b'ABCABCABC')
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
v = bytearray(b'ABC') * 100000000
print(v)
# OSError: [Errno 29] I/O error