Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python

How to create fake user profiles using faker in Python

Ravi

The faker library

In basic terms, the faker library generates fake data. This library can be used to create fake email IDs, profile information, and so on.

We use pip to install faker:

pip install faker

Providers

The faker module contains different functions to generate dummy data. It delegates the task of data generation to providers. Each provider is used to generate specific dummy data.

One such provider is profile. The profile provider is a collection of functions to generate personal profiles and identities.

The simple_profile() method

The simple_profile() method of the profile provider is used to generate a basic profile with personal information.

The attributes of the profile generated are as follows:

  • username
  • name
  • sex
  • address
  • mail
  • birthdate

Method signature

simple_profile(sex: Optional[GenderType] = None) → Dict[str, Union[str, datetime.date, GenderType]]

Parameters

  • sex: This is the sex of the profile to be generated. This is an optional argument. The character M is used to indicate male, and F is used to indicate female.

Refer to Standard Providers to get the standard providers in the faker module.

Example

import faker

faker_obj = faker.Faker()

gender = 'M'
male_profile = faker_obj.simple_profile(sex=gender)
print("Male profile generated is as follows:\n", male_profile)

gender = 'F'
male_profile = faker_obj.simple_profile(sex=gender)
print("Female profile generated is as follows:\n", male_profile)

Explanation

  • Line 1: We import the faker module.
  • Line 3: We create an object of the faker class.
  • Lines 5–7: We generate a male profile using the simple_profile() method with the sex argument as M.
  • Lines 9–11: We generate a female profile using the simple_profile() method with the sex argument as F.

RELATED TAGS

python
RELATED COURSES

View all Courses

Keep Exploring