Trying to solve contacts database in python core | Sololearn: Learn to code for FREE!

0

Trying to solve contacts database in python core

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 My replaces 00 with + even in the middle which is not meant to be import re number=input() str=number pattern="00" newstr=re.sub(pattern,"+",str) print(newstr) #your code goes here

4/17/2021 11:54:14 AM

Simisola Osinowo

6 Answers

New Answer

+4

#This amendment in your original code worked for me: import re inp=str(input()) pattern=r"00" mat=re.search(pattern,inp) if mat and mat.start()==0: # <-- change is here! print(re.sub(pattern,'+',inp,1)) else: print(int(inp)) """"Sample Input 0014860098 Sample Output +14860098"""

+2

import re #your code goes here number = input() pattern = r"00" if re.match(pattern, number): print(re.sub(pattern, "+", number)) else: print(number)

+1

Show your attempt bro

0

import re number=input() str=number pattern="00" newstr=re.sub(pattern,"+",str) print(newstr) #your code goes here

0

0

I used match and then treated the phone number string as a list and just sliced off the first two characters. To slice off the zeros,use entry[2:]. This leaves a string without the “00” at the beginning. Then add a “+” to the beginning of the string. This can be done in one line import re #your code goes here entry = input() if re.match("00",entry): entry = "+" + entry[2:] print(entry)