In this article, we will learn how to generate a random phone number using Python. In general, Indian phone numbers are of 10 digits and start with 9, 8, 7, or 6.
Approach:
- We will use the random library to generate random numbers.
- The number should contain 10 digits.
- The first digit should start with 9 or 8 or 7 or 6, we will use randint() method.
- The remaining 9 digits will also be generated using the randint() method.
Examples:
The generated random phone numbers would looklike:
9980231467
8726189362
Implementation:
Python3
import random as r
ph_no = []
ph_no.append(r.randint( 6 , 9 ))
for i in range ( 1 , 10 ):
ph_no.append(r.randint( 0 , 9 ))
for i in ph_no:
print (i, end = "")
|
Output:
8349603502