Faker Library in Python

Faker Library in Python is used to generate fake data in our program. There are many methods defined in this library that we can use to produce a fake name, id, date, time, email, location, etc. Let’s see more about this faker library further in this tutorial.

Faker Library in Python: Simple Examples

Before using this library, you must install it using the following command.

pip install faker

Now we are ready to use the faker library. Let’s see what we can do with it.

  • Some of the most common examples of the faker library include generating fake text, name, address, country, email, job, etc. We can use these to create a JSON file with fake data. Have a look at the following code to understand the concept.
    from faker import Faker
    
    fakeit = Faker()
    
    print(fakeit.name())
    print('-------------')
    print(fakeit.address())
    print('-------------')
    print(fakeit.email())
    print('-------------')
    print(fakeit.country())
    print('-------------')
    print(fakeit.job())
    print('-------------')
    print(fakeit.text())

    The above code gives the output as:

    Kirsten Miller
    -------------
    6374 Cody Courts
    Smithfurt, DC 79353
    -------------
    [email protected]
    -------------
    Hungary
    -------------
    Psychologist, educational
    -------------
    Skill base whose result identify process base fight. Miss rest support fill weight law. Very situation three major area issue.
  • We can use the profile() method with fakeit object to generate a fake profile as shown in the below code.
    from faker import Faker
    
    fakeit = Faker()
    
    print(fakeit.profile())

    Output:

    {'job': 'Tour manager', 'company': 'Walker-Sosa', 'ssn': '157-35-8315', 'residence': '455 Shawn Squares Suite 939\nWatsonland, IN 67175', 'current_location': (Decimal('-62.3030095'), Decimal('14.494937')), 'blood_group': 'A+', 'website': ['http://www.clark-howard.com/'], 'username': 'lgarcia', 'name': 'Shannon Dixon', 'sex': 'M', 'address': '298 Sarah Ports\nNorth Russell, UT 66887', 'mail': '[email protected]', 'birthdate': datetime.date(1921, 5, 20)}
  • We can also generate fake date and time values. Have a look at the code.
    from faker import Faker
    
    fakeit = Faker()
    
    #generating fake date and time
    
    print(fakeit.century())
    print('-------------')
    print(fakeit.year())
    print('-------------')
    print(fakeit.month())
    print('-------------')
    print(fakeit.day_of_month())

    The output of the above code:

    XVII
    -------------
    1994
    -------------
    06
    -------------
    28
  • Using the faker library we can create a series of fake sentences. See the below code.
    from faker import Faker
    
    fakeit = Faker()
    for i in range(10):
        print(fakeit.sentence())

    Output:

    Focus agreement member ask know itself knowledge top.
    Nice try while common.
    Trade short experience student seem public crime successful.
    Occur continue employee magazine police effect cultural.
    Down phone will series especially north.
    Member price brother message middle skin per.
    Set three these cause trouble store itself.
    Series production daughter property indicate.
    Somebody realize matter style physical cut.
    Pattern present physical bad real choice language.

Thank you.

Also read: Fuzzy String Matching in Python

Leave a Reply

Your email address will not be published. Required fields are marked *