Python String Method – replace()

Spread the love

replace() method –

The replace() method replace each matching characters from a string with a new characters.

syntax –

string.replace(old, new, [, count])

old – old substring you want to replace

new – new substring with which you want to replace

count (optional) – the number of times you want to replace the old substring with the new one.

Example –

In [1]: string1 = 'python'

In [2]: string1.replace('p', 'c')
Out[2]: 'cython'

In [3]: string2 = 'python is awesome. we love python. python is great'

In [4]: string2.replace('python', 'java', 2)
Out[4]: 'java is awesome. we love java. python is great'

Related Posts –

  1. Python String Method – Capitalize()
  2. Python String Method – title()
  3. Python String Method – center()
  4. Python String Method – count()
  5. Python String Method – startswith()
  6. Python String Method – endswith()
  7. Python String Method – find()
  8. Python String Method – index()
  9. Python String Method – isalnum()
  10. Python String Method – isalpha()
  11. Python String Method – isdecimal()
  12. Python String Method – isdigit()
  13. Python String Method – islower()
  14. Python String Method – istitle()
  15. Python String Method – isupper()
  16. Python String Method – join()
  17. Python String Method – lower()
  18. Python String Method – upper()
  19. Python String Method – swapcase()
  20. Python String Method – lstrip()
  21. Python String Method – rstrip()
  22. Python String Method – strip()

Rating: 1 out of 5.

Leave a Reply