Developer Tools for Random Data Generation // v2.6.1
root@generate-random:~/phone-numbers/python$ _

Generate Random Phone Numbers in Python

Complete code tutorial with examples and best practices

[ Code Example - Quick Summary ]

Language: Python

What: Generate random phone numbers in Python using random.randint() and Faker library. Covers US format, international format, and validation for testing.

Try it: Use our interactive Phone-numbers generator or integrate this code into your Python application.

Generate random phone numbers in Python using random.randint() and Faker library. Covers US format, international format, and validation for testing. Looking for other languages? Check our code examples in PHP , JavaScript , Java , C# , C++ , Ruby and Go or use our interactive web generator.

Python Code Example

import random
import re

# Generate random US phone number (xxx) xxx-xxxx
def random_us_phone_number():
    area_code = random.randint(200, 999)
    exchange = random.randint(200, 999)
    line_number = random.randint(1000, 9999)
    return f"({area_code:03d}) {exchange:03d}-{line_number:04d}"

# Generate random phone number with country code
def random_international_phone(country_code="+1"):
    area_code = random.randint(200, 999)
    exchange = random.randint(200, 999)
    line_number = random.randint(1000, 9999)
    return f"{country_code} ({area_code:03d}) {exchange:03d}-{line_number:04d}"

# Generate phone number without formatting
def random_phone_digits(length=10):
    return "".join(str(random.randint(0, 9)) for _ in range(length))

# Validate US phone number format
def is_valid_us_phone(phone):
    pattern = r"^\(\d{3}\) \d{3}-\d{4}$"
    return re.match(pattern, phone) is not None

# Generate multiple phone numbers
def generate_phone_numbers(count=10):
    return [random_us_phone_number() for _ in range(count)]

# Using Faker library (pip install faker)
# from faker import Faker
# faker = Faker()
# phone = faker.phone_number()  # (555) 123-4567
# phone_e164 = faker.msisdn()   # 15551234567 (E.164 without +)

print(f"US Phone: {random_us_phone_number()}")
print(f"International: {random_international_phone(\"+44\")}")
print(f"Digits only: {random_phone_digits(10)}")
print(f"Valid: {is_valid_us_phone(\"(555) 123-4567\")}")
print(f"Batch: {\", \".join(generate_phone_numbers(3))}")

[EXPLANATION]

Python uses random.randint() for generating area codes (200-999), exchanges (200-999), and line numbers (1000-9999). F-string formatting with :03d ensures 3-digit zero-padding. re.match() validates patterns. Faker library offers phone_number() for locale-specific formats and msisdn() for E.164 format (without + prefix). List comprehensions provide concise batch generation.

Expected Output

US Phone: (555) 234-5678
International: +44 (555) 234-5678
Digits only: 5551234567
Valid: True
Batch: (234) 567-8901, (345) 678-9012, (456) 789-0123

Common Use Cases

  • Generate phone numbers for Django/Flask user testing
  • Create contact data for Python-based CRM systems
  • Populate SQLAlchemy models with phone number test data
  • Test phone validation in web scraping applications
  • Generate mock data for Twilio API integration testing

Important Notes

  • F-string formatting f"{num:03d}" pads with leading zeros
  • Faker supports locales: Faker("en_US"), Faker("en_GB")
  • re.match() returns Match object or None (use is not None)
  • MSISDN is the full phone number including country code (E.164 format)
  • List comprehensions are more Pythonic than for loops for generation

Try Our Interactive Generator

Don't want to write code? Use our free web-based Phone-numbers generator with instant results.

TRY PHONE-NUMBERS GENERATOR →