Generate random phone numbers using python

If you want to generate random phone numbers for your website, this article is for you. With the help of a python package called phone-gen we can easily generate valid phone numbers just by giving the name of the country as the input.

Of course, we can randomly generate a phone number using 10 individual digits, but the number will not be valid. This library gives us a valid phone number with country code.

Let us get started with the installation of the package.

Installation

This package can be easily installed using pip like this. Just type the following command in your terminal.

pip install phone-gen

Examples

Let us see the examples to generate a random phone number or to get the country code alone. There are three examples.

  1. Generate a random phone number with country code.
  2. Get country code alone.
  3. Generate a random phone number without the country code.

1. Generate a random phone number with country code

The following code snippet gives us a random phone number.

This will print us the phone number like this.

+442908124840

2. Get country code alone

If we want the country code alone we can do something like this.

This will give us the country code alone.

44

3. Generate a random phone number without the country code

If we want the phone number alone without the country code, we can do something like this. Just add the following argument

full=False

to the get_number() method.

This will give us the phone number alone like this.

183782623

Conclusion

This article will be useful if you want to generate random valid mobile numbers for your project.

Happy coding !