Tag Archives: replace method

How to replace a substring in a string object in Python

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

>>> s1 = 'Dan is 39 years old. 39 is a nice age although other people think that you are old when you are 39 years old
>>> 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