Generate Random Phone Number with Country Code

  • Share this:

Code introduction


This function uses the Mimesis library's Person module to generate a random phone number and formats it based on the country code.


Technology Stack : Mimesis library, Python

Code Type : Function

Code Difficulty : Intermediate


                
                    
def generate_random_phone_number(country_code='US'):
    from mimesis import Person, Address
    phone_number = Person().phone()
    if country_code == 'US':
        return phone_number
    else:
        return f'+{country_code} {phone_number}'