*Memo for a string and byte string(bytes
and bytearray
) functions:
- My post explains removeprefix() and removesuffix().
- My post explains startswith().
- My post explains endswith().
*Memo for a string and byte string(bytes
and bytearray
):
str.replace() and bytes.replace() or bytearray.replace() can replace the zero or more substrings and byte substrings respectively matched to old
with new
from the left to the right as shown below:
*Memo:
- The 1st argument is
old
(Required-Type:str
forstr.replace()
or Bytes-like object forbytes.replace()
andbytearray.replace()
):- It's the substring and byte substring of the zero or more characters and bytes replaced with
new
respectively. - Don't use
old=
.
- It's the substring and byte substring of the zero or more characters and bytes replaced with
- The 2nd argument is
new
(Required-Type:str
forstr.replace()
or Bytes-like object forbytes.replace()
andbytearray.replace()
):- It's the substring and byte substring of the zero or more characters and bytes replacing
old
respectively. - Don't use
old=
.
- It's the substring and byte substring of the zero or more characters and bytes replacing
- The 3rd argument is
count
(Optional-Default:-1
-Type:int
):- It decides how many matched substrings and byte substrings are replaced respectively.
- If it's
-1
, allold
substrings and byte substrings are replaced respectively. - Don't use
count=
.
<String>:
v = "It's very very good"
print(v.replace('er', 'ER'))
print(v.replace('er', 'ER', -1))
print(v.replace('er', 'ER', 2))
# It's vERy vERy good
print(v.replace('er', 'ER', 1))
# It's vERy very good
print(v.replace('very', 'VERY'))
print(v.replace('very', 'VERY', 2))
# It's VERY VERY good
print(v.replace('very', 'VERY', 1))
# It's VERY very good
print(v.replace('', '--'))
# --I--t--'--s-- --v--e--r--y-- --v--e--r--y-- --g--o--o--d--
# ↓↓
print(v.replace('', ' '))
# I t ' s v e r y v e r y g o o d
# ↓ ↓↓
print(v.replace(' ', ' '))
# It's very very good
# ↓↓ ↓↓
print(v.replace(' ', ' '))
print(v.replace('', ''))
print(v.replace('er', 'ER', 0))
print(v.replace('very', 'VERY', 0))
print(v.replace('ER', 'ER'))
print(v.replace('VERY', 'VERY'))
print(v.replace('really', 'VERY'))
# It's very very good
<Byte String>:
bytes:
v = b"It's very very good"
print(v.replace(b'er', b'ER'))
print(v.replace(bytearray(b'er'), bytearray(b'ER')))
print(v.replace(b'er', b'ER', -1))
print(v.replace(bytearray(b'er'), bytearray(b'ER'), -1))
print(v.replace(b'er', b'ER', 2))
print(v.replace(bytearray(b'er'), bytearray(b'ER'), 2))
# b"It's vERy vERy good"
print(v.replace(b'er', b'ER', 1))
print(v.replace(bytearray(b'er'), bytearray(b'ER'), 1))
# b"It's vERy very good"
print(v.replace(b'very', b'VERY'))
print(v.replace(bytearray(b'very'), bytearray(b'VERY')))
print(v.replace(b'very', b'VERY', 2))
print(v.replace(bytearray(b'very'), bytearray(b'VERY'), 2))
# b"It's VERY VERY good"
print(v.replace(b'very', b'VERY', 1))
print(v.replace(bytearray(b'very'), bytearray(b'VERY'), 1))
# b"It's VERY very good"
print(v.replace(b'', b'--'))
print(v.replace(bytearray(b''), bytearray(b'--')))
# b"--I--t--'--s-- --v--e--r--y-- --v--e--r--y-- --g--o--o--d--"
# ↓↓
print(v.replace(b'', b' '))
print(v.replace(bytearray(b''), bytearray(b' ')))
# b" I t ' s v e r y v e r y g o o d "
# ↓ ↓↓
print(v.replace(b' ', b' '))
print(v.replace(bytearray(b' '), bytearray(b' ')))
# b"It's very very good"
# ↓↓ ↓↓
print(v.replace(b' ', b' '))
print(v.replace(bytearray(b' '), bytearray(b' ')))
print(v.replace(b'', b''))
print(v.replace(bytearray(b''), bytearray(b'')))
print(v.replace(b'er', b'ER', 0))
print(v.replace(bytearray(b'er'), bytearray(b'ER'), 0))
print(v.replace(b'very', b'VERY', 0))
print(v.replace(bytearray(b'very'), bytearray(b'VERY'), 0))
print(v.replace(b'ER', b'ER'))
print(v.replace(bytearray(b'ER'), bytearray(b'ER')))
print(v.replace(b'VERY', b'VERY'))
print(v.replace(bytearray(b'VERY'), bytearray(b'VERY')))
print(v.replace(b'really', b'VERY'))
print(v.replace(bytearray(b'really'), bytearray(b'VERY')))
# b"It's very very good"
bytearray:
v = bytearray(b"It's very very good")
print(v.replace(b'er', b'ER'))
print(v.replace(bytearray(b'er'), bytearray(b'ER')))
print(v.replace(b'er', b'ER', -1))
print(v.replace(bytearray(b'er'), bytearray(b'ER'), -1))
print(v.replace(b'er', b'ER', 2))
print(v.replace(bytearray(b'er'), bytearray(b'ER'), 2))
# bytearray(b"It\'s vERy vERy good")
print(v.replace(b'er', b'ER', 1))
print(v.replace(bytearray(b'er'), bytearray(b'ER'), 1))
# bytearray(b"It\'s vERy very good")
print(v.replace(b'very', b'VERY'))
print(v.replace(bytearray(b'very'), bytearray(b'VERY')))
print(v.replace(b'very', b'VERY', 2))
print(v.replace(bytearray(b'very'), bytearray(b'VERY'), 2))
# bytearray(b"It\'s VERY VERY good")
print(v.replace(b'very', b'VERY', 1))
print(v.replace(bytearray(b'very'), bytearray(b'VERY'), 1))
# bytearray(b"It\'s VERY very good")
print(v.replace(b'', b'--'))
print(v.replace(bytearray(b''), bytearray(b'--')))
# bytearray(b"--I--t--\'--s-- --v--e--r--y-- --v--e--r--y-- --g--o--o--d--")
# ↓↓
print(v.replace(b'', b' '))
print(v.replace(bytearray(b''), bytearray(b' ')))
# bytearray(b" I t \' s v e r y v e r y g o o d ")
# ↓ ↓↓
print(v.replace(b' ', b' '))
print(v.replace(bytearray(b' '), bytearray(b' ')))
# bytearray(b"It\'s very very good")
# ↓↓ ↓↓
print(v.replace(b' ', b' '))
print(v.replace(bytearray(b' '), bytearray(b' ')))
print(v.replace(b'', b''))
print(v.replace(bytearray(b''), bytearray(b'')))
print(v.replace(b'er', b'ER', 0))
print(v.replace(bytearray(b'er'), bytearray(b'ER'), 0))
print(v.replace(b'very', b'VERY', 0))
print(v.replace(bytearray(b'very'), bytearray(b'VERY'), 0))
print(v.replace(b'ER', b'ER'))
print(v.replace(bytearray(b'ER'), bytearray(b'ER')))
print(v.replace(b'VERY', b'VERY'))
print(v.replace(bytearray(b'VERY'), bytearray(b'VERY')))
print(v.replace(b'really', b'VERY'))
print(v.replace(bytearray(b'really'), bytearray(b'VERY')))
# bytearray(b"It\'s very very good")
Top comments (0)