What’s the best way to format a phone number in Python?

Posted on

Question :

What’s the best way to format a phone number in Python?

If all I have is a string of 10 or more digits, how can I format this as a phone number?

Some trivial examples:


I know those aren’t the only ways to format them, and it’s very likely I’ll leave things out if I do it myself. Is there a python library or a standard way of formatting phone numbers?

Asked By: Joe


Answer #1:

for library: phonenumbers (pypi, source)

Python version of Google’s common library for parsing, formatting, storing and validating international phone numbers.

The readme is insufficient, but I found the code well documented.

Answered By: Joe

Answer #2:

Seems like your examples formatted with three digits groups except last, you can write a simple function, uses thousand seperator and adds last digit:

>>> def phone_format(n):                                                                                                                                  
...     return format(int(n[:-1]), ",").replace(",", "-") + n[-1]                                                                                                           
>>> phone_format("5555555")
>>> phone_format("5555555")
>>> phone_format("5555555555")
>>> phone_format("18005555555")
Answered By: kusut

Answer #3:

Here’s one adapted from utdemir’s solution and this solution that will work with Python 2.6, as the “,” formatter is new in Python 2.7.

def phone_format(phone_number):
    clean_phone_number = re.sub('[^0-9]+', '', phone_number)
    formatted_phone_number = re.sub("(d)(?=(d{3})+(?!d))", r"1-", "%d" % int(clean_phone_number[:-1])) + clean_phone_number[-1]
    return formatted_phone_number
Answered By: utdemir

Answer #4:

You can use the function clean_phone() from the library DataPrep. Install it with pip install dataprep.

>>> from dataprep.clean import clean_phone
>>> df = pd.DataFrame({'phone': ['5555555', '5555555555', '18005555555']})
>>> clean_phone(df, 'phone')
Phone Number Cleaning Report:                                                   
    3 values cleaned (100.0%)
Result contains 3 (100.0%) values in the correct format and 0 null values (0.0%)
         phone     phone_clean
0      5555555        555-5555
1   5555555555    555-555-5555
2  18005555555  1-800-555-5555
Answered By: Jon Mabe

Answer #5:

A simple solution might be to start at the back and insert the hyphen after four numbers, then do groups of three until the beginning of the string is reached. I am not aware of a built in function or anything like that.

You might find this helpful:

Regular expressions will be useful if you are accepting user input of phone numbers. I would not use the exact approach followed at the above link. Something simpler, like just stripping out digits, is probably easier and just as good.

Also, inserting commas into numbers is an analogous problem that has been solved efficiently elsewhere and could be adapted to this problem.

Answered By: victoria55

Leave a Reply

Your email address will not be published. Required fields are marked *