Tag Archives: replace method in python

How to replace a substring in a string object in Python

Let’s consider that you need to replace a substring with a new substring within a string object.

>>> s1 = 'Dan is 39 years old. 39 is a nice age although other people think that you are old when you are 39 yea
>>> s2 = s1.replace('39', '40')
>>> s2
'Dan is 40 years old. 40 is a nice age although other people think that you are old when you are 40 years old.'

Continue reading