Faker module introduction

Posted by Afsal on 17-Mar-2023

Hi Pythonistas!

Today we are going to learn a new package called Faker. This is very helpful for generating test data, pandas data frames, etc. This package has localization support also. Let us learn with an example


pip install Faker


from faker import Faker
import pprint
pp = pprint.PrettyPrinter(indent=4)

fake = Faker()
user_data = []
for _ in range(10):
    user = {
        "name": fake.name(),
         "email": fake.email(),
         "company": fake.company(),
         "address": fake.address(),
         "date_of_birth": fake.date_of_birth()



[   {   'address': '34901 Jeremy Road Suite 500\nThomasport, LA 86259',

     'company': 'Hansen-Hansen',

     'date_of_birth': datetime.date(1930, 6, 7),

     'email': 'scarr@example.net',

     'name': 'Michael Hayden'},

{   'address': '0616 Kent Brooks Apt. 147\nWest Edwin, UT 57731',

     'company': 'Schroeder Ltd',

     'date_of_birth': datetime.date(1987, 5, 29),

     'email': 'joshualeonard@example.net',

     'name': 'Faith Johnson'},

{   'address': '121 Harold Shoals Apt. 026\nNorth Tyler, AS 16405',

     'company': 'Porter and Sons',

     'date_of_birth': datetime.date(2011, 5, 23),

     'email': 'brianevans@example.net',

     'name': 'Kevin Jackson'},

{   'address': '14839 Mcbride Fields\nWest Marieburgh, LA 92227',

     'company': 'Ortiz and Sons',

     'date_of_birth': datetime.date(1976, 10, 3),

     'email': 'vperry@example.com',

     'name': 'Brian Franklin'},

{   'address': '3024 Regina Gardens\nCainberg, AK 17863',

     'company': 'Leon-Leach',

     'date_of_birth': datetime.date(1982, 8, 6),

     'email': 'deborah36@example.com',

     'name': 'Daniel Pearson'},

{   'address': '7542 Anne Trafficway Apt. 633\nLake Sarahshire, MO 19797',

     'company': 'Aguirre-Ballard',

     'date_of_birth': datetime.date(1925, 3, 22),

     'email': 'mcphersonharry@example.org',

     'name': 'Jennifer Baker'},

{   'address': '9901 Williams Circle Apt. 196\nSchneiderborough, VI 41081',

     'company': 'Chavez PLC',

     'date_of_birth': datetime.date(1998, 9, 13),

     'email': 'pjackson@example.org',

     'name': 'Michelle Smith'},

{   'address': 'USS Camacho\nFPO AP 61797',

     'company': 'Vargas Ltd',

     'date_of_birth': datetime.date(1908, 10, 19),

     'email': 'velazquezmichael@example.com',

     'name': 'Kimberly Briggs'},

{   'address': '902 Evans Park\nJanetburgh, NY 12153',

     'company': 'Grimes, Morrison and Garcia',

     'date_of_birth': datetime.date(2002, 1, 12),

     'email': 'charlesmartinez@example.org',

     'name': 'Victoria Brewer'},

{   'address': '09336 Megan Lights Suite 699\nPort Jamesfort, ND 92366',

     'company': 'Lewis LLC',

     'date_of_birth': datetime.date(1979, 12, 11),

     'email': 'dlewis@example.net',

     'name': 'Anna Paul'}]

In the above code we can see that it will create 10 user records with very few lines of code.

Now we can see the localization support.


from faker import Faker
fake = Faker(['ja_JP'])

for _ in range(10):


橋本 真綾

松田 亮介

田中 あすか

吉田 舞

小林 充

山田 幹

渡辺 零

前田 健一

近藤 あすか

佐藤 里佳

Here in this example we have set language as Japanese so it creates a name in Japanese character. 

I hope you have learned something from this post. Please share your valuable suggestions with afsal@parseltongue.co.in