- 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
17 Réponses
+ 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"""
+ 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)
+ 2
what is the essence of putting the sign "^" before the 00
as in r"^00"
+ 1
Show your attempt bro
+ 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)
+ 1
import re
#your code goes here
input_str = input()
pattern = r'^[0]{2}'
res = re.sub(pattern, '+', input_str)
print(res)
+ 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)
0
import re
number=input()
str=number
pattern="00"
newstr=re.sub(pattern,"+",str)
print(newstr)
#your code goes here
0
Thanks
0
number = input()
pattern = r"00"
if re.match(pattern, number):
print(re.sub(pattern, "+", number))
else:
print(number)
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)
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)
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)
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))
0
import re
number=input()
pattern=r"^00"
newstr=re.sub(pattern,"+",number)
print(newstr)
#your code goes here
0
import re
num = input()
num=re.sub(r"^00","+",num)
print(num)
#your code goes here
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)