DEV Community

TechPlygrnd
TechPlygrnd

Posted on

How to Generate Fake Data in Python With Faker

Do you often feel annoyed when trying to make made up data for testing your Python application such as names and addresses? Well, worry no more, now you can generate those data automatically using Faker package.


Install Faker Dependency

To use the package, you have to install it first using the following command

pip install Faker
Enter fullscreen mode Exit fullscreen mode

Create fake data

After installing the package into your machine, you can start to use it by importing it into your code

from faker import Faker
Enter fullscreen mode Exit fullscreen mode

After that create an object from the Faker class

fake = Faker()
Enter fullscreen mode Exit fullscreen mode

Then you can start to use it, for example in here I will try to generate a fake name

fake_name = fake.name()
print(fake_name)
Enter fullscreen mode Exit fullscreen mode

From the output, you will see that the code will generate a fake name (your output might be different)

Tony Leon
Enter fullscreen mode Exit fullscreen mode

I will use loop to generate 10 fake names

for _ in range(10):
  print(fake.name())
Enter fullscreen mode Exit fullscreen mode

You can see from the output that 10 fake names are generated

Jessica Griffin
Richard Carson
Alyssa Wilson
Jonathan Chandler
Clinton Riley
Jeffrey Moore
Lisa Coffey
Pamela Swanson
Carl Jones
Todd Guzman
Timothy Kim
Collin Dillon
Lisa Hall
Adam Robinson
Michael Richardson
Brandy Webb MD
Anthony Tate
Tyler Alvarado
Alyssa Lee
Heather Robbins
Enter fullscreen mode Exit fullscreen mode

Not only generating a fake name, you can also generate a fake address and a fake text.

I will generate a fake address

fake_address = fake.address()
print(fake_address)
Enter fullscreen mode Exit fullscreen mode

The output shows a fake address

0084 Patel Crossing
New Eric, CT 79853
Enter fullscreen mode Exit fullscreen mode

Below code will generate fake text

fake_text = fake.text()
print(fake_text)
Enter fullscreen mode Exit fullscreen mode

Below are the output of the generated fake text

Into side begin compare affect. Like fall generation.
Consider TV sell something method most.
Message personal fund attack sea social.
Enter fullscreen mode Exit fullscreen mode

Providers

Provider refer to a faker generator. You can use it like this

from faker import Faker
from faker.providers import internet

fake = Faker()
fake.add_provider(internet)

for _ in range(10):
  print(fake.ipv4_private())
Enter fullscreen mode Exit fullscreen mode

You can see the list of providers provided by the faker package built-in by this link https://faker.readthedocs.io/en/stable/providers.html#standard-providers and also the providers that created by the community https://faker.readthedocs.io/en/stable/communityproviders.html#community-providers

Localization

You can set the localization according to your preference. For example, if you want to generate fake Japanese names, you can set it by the following code

from faker import Faker

fake = Faker('ja_JP')

for _ in range(10):
  print(fake.name())
Enter fullscreen mode Exit fullscreen mode

Below are the result of the code

鈴木 幹
藤井 さゆり
鈴木 幹
渡辺 結衣
長谷川 洋介
山崎 浩
山本 和也
渡辺 結衣
長谷川 美加子
中村 智也
Enter fullscreen mode Exit fullscreen mode

Faker v3.0.0 supports multiple locales, so if you want to generate the combination of Japanese and US names

from faker import Faker

fake = Faker(['en_US', 'ja_JP'])

for _ in range(10):
  print(fake.name())
Enter fullscreen mode Exit fullscreen mode

The output is the following

Jerry Chavez
伊藤 真綾
Phillip Williams
Elizabeth Wood
Michelle Villarreal
森 康弘
Melanie James
Mr. Andrew Glenn
山田 結衣
田中 七夏
Enter fullscreen mode Exit fullscreen mode

You can read the following page for the supported locales https://faker.readthedocs.io/en/master/locales.html


There you go, that is how to can utilize Faker package in Python to generate fake data. I hope this blog helps you, see you next time!

Top comments (0)