replace in Python
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

replace in Python

Publish Date: Aug 4 '25
0 0

Buy Me a Coffee

*Memo:

  • My post explains string, bytes and bytearray functions.

str.replace() and bytes.replace() or bytearray.replace() can respectively replace the substrings and byte substrings matched to old with new from the left to the right as shown below:

*Memo:

  • The 1st argument is old(Required-Type:str for str.replace() or Bytes-like object for bytes.replace() and bytearray.replace()):
    • It's the substring and byte substring of the zero or more characters and bytes replaced with new respectively.
    • Don't use old=.
  • The 2nd argument is new(Required-Type:str for str.replace() or Bytes-like object for bytes.replace() and bytearray.replace()):
    • It's the substring and byte substring of the zero or more characters and bytes replacing old respectively.
    • Don't use old=.
  • 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, all old 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
Enter fullscreen mode Exit fullscreen mode

<Bytes & Bytearray>:

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"
Enter fullscreen mode Exit fullscreen mode

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")
Enter fullscreen mode Exit fullscreen mode

Comments 0 total

    Add comment