Search code examples
pythonsmspython-phonenumber

How can I use the python-phonenumbers library to check the type of phone number?


How can we use the python-phonenumbers library to determine whether a particular phone number is a mobile number or landline number?


Solution

  • Use phonenumbers.phonenumberutil.number_type to get the number type (after you have parsed the number). e.g.

    x = phonenumbers.parse("0282784492", "AU")
    phonenumbers.phonenumberutil.number_type(x)
    

    So if you wanted to use x if it was not a fixed line number you could do:

    if phonenumbers.phonenumberutil.number_type(x) is not phonenumbers.PhoneNumberType.FIXED_LINE:
        # Do something...
    

    The possible phone number types are:

    • FIXED_LINE = 0
    • MOBILE = 1
    • FIXED_LINE_OR_MOBILE = 2
    • TOLL_FREE = 3
    • PREMIUM_RATE = 4
    • SHARED_COST = 5
    • VOIP = 6
    • PERSONAL_NUMBER = 7
    • PAGER = 8
    • UAN = 9
    • VOICEMAIL = 10
    • UNKNOWN = 99

    See the full descriptions here.