3

I'm getting a string from a form format 07123456789 (it's a phone number) and I would like to remove the first number/character only if it's a zero. And after that add 44 in front. So the output will be 447123456789.

Which is the best way to do it?

6
  • You should really add +44 to the front. Otherwise you cannot distinguish betewen phone numbers which started to have no 0, but a 44 at the start. (I suppose adding the 44 should only happen if there was a 0, otherwise not.) Commented Jan 3, 2015 at 10:24
  • Im using an API to send text messages. The accepted format of number is 447123456789. Commented Jan 3, 2015 at 10:42
  • Ok. But what happens (or: what should happen) if the number didn't start with 0 at the first place? Is it supposed to contain the prefix already or is it an error case? Commented Jan 3, 2015 at 10:45
  • the form that Im requesting the number has validation rules and will always starts with zero. Actually here I converting the local number to an international accepted number for the API. I would like the user to type his number as local to be much easier for him. Commented Jan 3, 2015 at 10:47
  • Ok, then all is fine. I just stumbled over your "I would like to remove the first number/character only if its a zero" part and wondered what would happen when it is not a zero. BTW, with these restrictions you disallow entering numbers from abroad. That may or may not be wanted, just wanted to point out... Commented Jan 3, 2015 at 10:52

5 Answers 5

13

Use str.startswith

In [11]: s = '07123456789'

In [12]: '44{}'.format(s[1:] if s.startswith('0') else s)
Out[12]: '447123456789'

Also, instead of formatting, you can join the strings together with + operator:

'44' + (s[1:] if s.startswith('0') else s)

If you're sure there's at most 1 zero at the beginning of the number, you can safely use str.lstrip or int conversion (see other answers).

Sign up to request clarification or add additional context in comments.

Comments

5

We can convert string into integer e.g.

>>> s = "07123456789"
>>> "44%s"%(int(s))
'447123456789'

We can use lstrip method of string which will remove all "0" from left of string. e.g.

>>> "44%s"%(s.lstrip("0"))
'447123456789'

If we want to consider only the first character from string then we can try following: (above two solution will not when more then one "0" at starting of string.)

>>> if s[0]=="0":
...   s = s[1:]
... 
>>> "44%s"%s
'447123456789'

Or go with solution from jamylak

>>> s = '07123456789'
>>> "44%s%s"%(s[0].strip("0"), s[1:])
'447123456789'

4 Comments

I like lstrip in this case. seems like the exact tool for the job
@jamylak not when the phone number has more than one 0 at the beginning... I removed the lstrip from my answer for this very reason.
I thought about that, then i thought the phone numbers probably don't have more than 1 0 but that's being naiive
yes, lstrip and int are not work when string has more than one "0" at the beginning, it will remove all "0" from the beginning.
4

Just another possible option:

>>> s = '07123456789'
>>> '44' + s[0].strip('0') + s[1:]
'447123456789'

Comments

3

str.lstrip is best for this case:

>>> s = '07123456789'
>>> '44'+s.lstrip('0')
'447123456789'
>>> s = '7123456789'
>>> '44'+s.lstrip('0')
'447123456789'

Comments

1

One way to go about this is to cast to int to remove the zero at the beginning and then cast back to a str:

s = '07123456789'
s = '44'+str(int(s))

2 Comments

Plus 1. More general approach than using startswith, probably.
This will remove all the leading zeroes, while he asked for the first one.

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.