0

I need to complete a basic task on Python that requires me to convert a standard phone number into an international one.

So for example, if the users phone number was 0123456789, the program should display, Your international phone number is +44123456789.

I don't know how to replace the 0 with a 44. I don't know many techniques on Python so advice on how to is welcomed, thanks.

EDIT:

    #Python Number Conversion
def GetInternational(PhoneNumber):
    if num.startswith('0'):
        num = num.replace('0','+44',1)
        return GetInternational

PhoneNumber = input("Enter your phone number: ")
print('Your international number is',GetInternational,'')

I'm missing something obvious but not sure what...

0

2 Answers 2

4

Another way to do it would be:

num.replace('0','+44',1) #Only one, the leftmost zero is replaced

Therefore if the number starts with zero we replace only that one,

num = "0123456789"

if num.startswith('0'):
    num = num.replace('0','+44',1)
Sign up to request clarification or add additional context in comments.

Comments

3

Well the simplest way to do it is strip off the first character (the 0) and then concatenate it with +"44":

num = "0123456789"

if num.startswith("0"):
    num = "+44" + num[1:]

For clarity I added a startswith check to make sure the substitution only happens if the number starts with a zero.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.