- 1

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

17th Apr 2021, 11:54 AM
Simisola Osinowo
Simisola Osinowo - avatar
17 Respostas
+ 8
#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"""
17th Apr 2021, 12:17 PM
Aditya
Aditya - avatar
+ 3
import re #your code goes here number = input() pattern = r"00" if re.match(pattern, number): print(re.sub(pattern, "+", number)) else: print(number)
16th Aug 2021, 8:14 PM
Jo_Flow
+ 2
what is the essence of putting the sign "^" before the 00 as in r"^00"
21st Sep 2022, 9:30 AM
Adekunle Taiwo
+ 1
Show your attempt bro
17th Apr 2021, 11:58 AM
Aditya
Aditya - avatar
+ 1
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)
8th Aug 2021, 5:32 PM
Jeremy White
Jeremy White - avatar
+ 1
import re #your code goes here input_str = input() pattern = r'^[0]{2}' res = re.sub(pattern, '+', input_str) print(res)
8th Mar 2022, 12:27 AM
Chuks AJ
Chuks AJ - avatar
+ 1
I came up with this and it seems to pass all tests: import re num = str(input()) pattern = r"00" if num[:2] == "00": newNum = num.replace(num[:2], "+") print(newNum) else: num[:2] != "00" print(num)
15th Jan 2023, 9:33 PM
Oli
Oli - avatar
0
import re number=input() str=number pattern="00" newstr=re.sub(pattern,"+",str) print(newstr) #your code goes here
17th Apr 2021, 12:07 PM
Simisola Osinowo
Simisola Osinowo - avatar
0
Thanks
17th Apr 2021, 12:25 PM
Simisola Osinowo
Simisola Osinowo - avatar
0
number = input() pattern = r"00" if re.match(pattern, number): print(re.sub(pattern, "+", number)) else: print(number)
19th Nov 2021, 2:43 PM
Stefan Bartl
Stefan Bartl - avatar
0
Hi, the following is my solution:- import re Number=input("phone number: ") pattern = r"00" if re.match(pattern,Number): NewNumber = re.sub(pattern, "+", Number,1) print(NewNumber) else: print(Number)
22nd Nov 2021, 11:40 AM
Mahsa Khamoushi
Mahsa Khamoushi - avatar
0
#My solution is here. I wish help you.. import re phone_n = input("please enter your phone number: ") patt = r"00" match = re.search(patt, phone_n) if match: if match.start() == 0 and match.end() == 2: phone_n = re.sub(patt, "+", phone_n, count =1) print(phone_n)
4th Dec 2021, 9:00 PM
Bekir
Bekir - avatar
0
#This worked for me only in sololearn. But when run outsite here, error occurs in line 2. import re num=input() pattern=r"00" num2=re.search(pattern,num) if num2 and num2.start()==0: print(re.sub(pattern,'+',num,1)) else: print(num)
29th Jan 2022, 12:27 PM
JAMES MUTUNE
JAMES MUTUNE - avatar
0
import re str = input() pattern = r"00" num1= re.sub(pattern, "+" ,str) num2 = re.search(pattern,str) if num2 and num2.start() == 0: print(num1) else: print(int(str))
15th Aug 2022, 6:49 AM
Saeed Karrabi
Saeed Karrabi - avatar
0
import re number=input() pattern=r"^00" newstr=re.sub(pattern,"+",number) print(newstr) #your code goes here
27th Jan 2023, 11:43 AM
Anish Abraham
Anish Abraham - avatar
0
import re num = input() num=re.sub(r"^00","+",num) print(num) #your code goes here
8th Feb 2023, 2:20 PM
Анваров фарход
Анваров фарход - avatar
0
import re #your code goes here phone_number = input() pattern = "00" replace = "+" if re.match(pattern, phone_number): print(re.sub(pattern,replace,phone_number,1)) else: print(phone_number)
5th Jul 2023, 7:09 PM
Cornel Cristea