
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 –
- Python String Method – Capitalize()
- Python String Method – title()
- Python String Method – center()
- Python String Method – count()
- Python String Method – startswith()
- Python String Method – endswith()
- Python String Method – find()
- Python String Method – index()
- Python String Method – isalnum()
- Python String Method – isalpha()
- Python String Method – isdecimal()
- Python String Method – isdigit()
- Python String Method – islower()
- Python String Method – istitle()
- Python String Method – isupper()
- Python String Method – join()
- Python String Method – lower()
- Python String Method – upper()
- Python String Method – swapcase()
- Python String Method – lstrip()
- Python String Method – rstrip()
- Python String Method – strip()