01 / 07
What replace cannot do
In Chapter 2 you met replace. Write text.replace("020", "0161") and one fixed piece of text becomes another.
But what about "turn every digit into a *"? Digits run from 0 to 9, and how many of them there are varies. Ten calls to replace? Even then you could not treat a run of them as one.
What you want is to replace by shape. That is what re.sub is for.
text = "phone 020-7946-0123"print(text.replace("020", "0161"))Result
phone 0161-7946-0123