Can someone please explain to me why this works and other ways to do this using re? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can someone please explain to me why this works and other ways to do this using re?

Regular expressions We need to create a number formatting system for a contacts database. Create a program that will take the phone number as input, and if the number starts with "00", replace them with "+". The number should be printed after formatting. Sample Input 0014860098 Sample Output +14860098 Notice that here you shouldn't convert the input string to integer, because you will work with symbols. Code: import re num = input() print(re.sub(r"^00","+",num))

12th May 2022, 1:46 PM
Ava Alford
1 Answer
+ 1
It substitutes two 0's with a '+' IF the 0's are at the start of the number. The important part is the carat. It tests for items only at the beggining.
12th May 2022, 2:40 PM
Slick
Slick - avatar