Introduction
During application development, we often need data to populate different fields such as usernames, barcodes, ssn, and many more just for testing. In this tutorial, we will be looking at how to generate fake data using Python.
Install module
To begin we first need to install a module called Faker which will be used to generate fake data. To install the module run the command below on your terminal.
pip3 install Faker
Start
Now let’s create a file called main.py and write the code below.
from faker import Faker
# Create a Faker instance
fake = Faker()
Generate fake name
print("Name: ", fake.name())
Output
Name: Erica Drake
Generate fake address
print("Address: ", fake.address())
Output
Address: 847 Cline Lake Suite 995
Generate fake text
print("Text: ", fake.text())
Output
Text: As environment could large base would western. Something surface best others.
Say citizen accept common face. Statement mouth sell far read. Rule kitchen he girl week enough science system.
Generate fake sentence
print("Sentence: ", fake.sentence())
Output
Sentence: Responsibility month media part team matter.
Generate fake job
print("Job: ", fake.job())
Output
Job: Nurse, children's
Generate co-ordinates
print("Coordinate: ", fake.coordinate())
Output
Coordinate: -117.317459
Generate fake file extension
print("File extension: ", fake.file_extension())
Output
File extension: wav
Generate fake ISBN
print("ISBN: ", fake.isbn10())
Output
ISBN: 0-224-76529-9
Generate first male name
print("First name (male): ", fake.first_name_male())
Output
First name (male): Brian
Generate the last female name
print("Last name (female): ", fake.last_name_female())
Output
Last name (female): Carney
Generate SSN number
print("SSN: ", fake.ssn())
Output
SSN: 807-89-0204
Generate cryptocurrency
print("Cryptocurrency: ", fake.cryptocurrency())
Output
Cryptocurrency: ('PPC', 'Peercoin')
Generate barcode
print("Barcode: ", fake.ean(length=13))
Output
Barcode: 5708179696967
Generate fake license plate
print("License plate: ", fake.license_plate())
Output
License plate: KMC-557
There you have it, you can generate more data categories using the module, feel free to check the official documentation here. Thanks for reading. Happy Coding.