Python Faker Library

If you are learning a field that requires a lot of data to practice such as data analysis, data science, and machine learning then it can be challenging to find that data to use. Fortunately, a python developer has created a library called the faker library that can generate fake data. This article will help you have an idea for this library.

1. Fake Names

The faker library has a lot of things that can be fake such as names, addresses, emails, credit card info, and much more. Let’s first install the library in your IDE and start making some fake data:

pip3 install Faker

After the successful installation of the faker package, let’s move to make some fake names data:

from faker import Faker

# Initialize The Object
fake = Faker()

# Generate The Fake Name
fake_name = fake.name()

# Print The Fake Name
print(fake_name)

First, you will need to import the Faker module from the faker library and then create an object from the Faker module and call it fake. We will use the fake module to generate the fake name by accessing its properties, the .name() property. 

Finally, print the fake name with the print function. You will notice that every time you run the code, you will get a different result which means it is working, and you are getting fake data every time. 

Sometimes you need to have many fake names, and it won’t make sense to keep printing the fake_name variable every time, although it will work. Let’s see a better option to perform this action:

from faker import Faker

# Initialize The Object
fake = Faker()

# Generating Many Fake Names
for _ in range(5):
    
    # Print The Fake Name
    print(fake.name())

We will use the range function to keep looping over the fake.name() function will keep generating fake data in every loop. You can change the number of fake data in the range() function number.

2. Change The Language

The good thing I like about this library is that you can generate fake data in many languages, not only in English. Let’s try to change the language to the Czech language:

from faker import Faker

# Initialize The Object
fake = Faker('cz_CZ')

# Generating Many Fake Names
for _ in range(5):
    
    # Print The Fake Name
    print(fake.name())

When you run the code, you can see that you will get names with different characters than the English characters, which means we’ve successfully changed the language.

3. Fake Profiles

Sometimes you need to generate fake profiles when you need a lot of data to perform, for example, analysis when you are learning something, and there are many other purposes. Let’s see an example:

from faker import Faker

# Initialize The Object
fake = Faker()

# Print The Fake Profile
print(fake.profile())

When you execute the previous code, you will have a lot of data such as fake names, addresses, emails, blood type, job name, etc. The output is a python dictionary, and you can access its elements with this method:

from faker import Faker

# Initialize The Object
fake = Faker()

# Generate The Fake Data
profile = fake.profile()

This code is for generating the fake data and storing them as a python dictionary in the profile variable. In the next cell of your Jupyter Notebook, you can run this code:

print(profile["mail"])
print(profile["job"])

The reason why we’ve separated them is that whenever you run the code, you will get other data stored in the profile variable. So that you will get new data every time, and maybe that’s not what you want in your code. We’ve accessed the key of the dictionary to get the values.

4. Get All The Faker Methods

We’ve explored how to generate fake names and fake profiles but this library can do a lot more than that. I can’t try all of these because it doesn’t make sense to keep showing you all of them but there is a command that will show you all of the available things that the faker library can generate:

from faker import Faker

# Initialize The Object
fake = Faker()

# Show All Of The Availabe Methods
dir(fake)

You can see we’ve used the dir() function that is available in python to show you all of the available methods inside the faker library. There is a high number of methods and things that you can generate that we can’t explore.

5. Using Faker As a Function

When you want to use the fake library for example many times in your code, it would be better to create a function and just call it whenever you need it instead of typing the whole code every time. Let’s see an example:

from faker import Faker

# Initialize The Object
fake = Faker()

# Create a Function
def fake_names(number):    
    #create an Empty List
    names = []   
    # Generating Many Fake Names
    for _ in range(number):  
        # Adding The Names To The List
        names.append(fake.name())
    # Return The List
    return names

Create a function first, and let’s call it fake_names, and create an empty list to store the fake names later. Then we use the for loop to keep looping over the fake.name() function to generate many fake names and store them in the list with the .append() function. Finally, return the list.

Conclusion

Thanks for reading! The faker library is more extensive than what we’ve learned in this article. We’ve only explored how you can make fake names and addresses but you can do a lot more. You can explore more about this library in their official documentation.

Scroll to Top