DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Edited on

replace in Python

Buy Me a Coffee

*Memo for string, bytes and bytearray functions:

*Memo for a 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 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

Top comments (0)