Python fake name generator Using Python faker Library


Notice: Undefined index: limited_lang in /home/beaczwhx/dggulaitutorial.com/wp-content/plugins/code-syntax-highlighter/inc/src/rendrer.php on line 297

Hello coders welcome to my new tutorial Python Fake Name Generator. In this tutorial you will learn to generate names using python faker library. Here I will teach you how to generate names by different ways using faker library in python.

Faker is the most popular python library to generate dummy or fake datasets. In this tutorial you will learn following things –

  • Generate fake names
  • Generate fake namess of different countries in different language
  • Generate names’s prefix
  • Generate name’s suffix
  • Generate first names and last names etc.

So without any delay let’s get started this interesting tutorial.

Python Fake Name Generator Using Python faker Library

Installing Faker Library In Anaconda Prompt

To use faker library we first need to install it in our system. So run following command in your anaconda prompt. This will install faker library in your system.

pip install Faker

Generating Fake Names 

Now we will learn how to generate names using faker module. So let’s do it.

To generate fake data, we first need to create a faker object and then call methods to generate fake data as per our requirement.

Let’s understand it with code.

import faker

fake_data = Faker()

f_name = fake_data.name()

print("Full Name : ", f_name)

What We Did?

  • First of all we have imported faker module.
  • Then created an object of Faker class.
  • Now we have generated fake name by calling name() method by using the faker object.
  • And at the last we have printed the generated fake name.

We have written the code successfully, now lets run the code and check the output.

Python Fake Name Generator

So guys in the above output you can see a fake name is generated successfully.

Generating Localized Fake Name 

By default, faker generate data in English and US centric(en-US ). But we can also generate localized data. For eg. if we want to generate Indian names in English then we will have to pass locale parameter while creating the faker object.

Let’s see it with an example code.

import faker

fake_data = Faker(["en_IN"]) #add locale parameter

f_name = fake_data.name()

print("Full Name : ", f_name)
  • en_IN is for English(India)

Let’s see its output.

Python Fake Name Generator

So guys, you can see an Indian name is generated in English name. What if you wanted to generate Indian name in Hindi language. You can do it by passing hi_IN parameter. Let’s see it with an example.

import faker

fake_data = Faker(["hi_IN"])   #Add hi_IN for hindi(Indian)

f_name = fake_data.name()

print("Full Name : ", f_name)

Let’s see its output.

Python Fake Name Generator

Awesome !!! So we can generate names of any country in any language. If you want to see all the countries and languages then check this link.

Are you guys enjoying this python fake name generator tutorial? Yes, then keep reading !!!

Generating Multiple Fake Names

Till now we have only generated one name at a time but what if we require to generate multiple fake names. So let’s see how to do that.

Generating multiple fake names using faker module is very simple. We just have to start a for loop. Let’s understand it with an example.

import faker

fake_data = Faker()

for _ in range(5):
    f_name = fake_data.name()
    print("Full Name : ", f_name)
  • Here we have generated 5 fake names.

Output

Python Fake Name Generator

So we have generated multiple names using faker module. Here we have only generated 5 names, you can do whatever you want.

Generating Male And Female Name

We can generate male and female names using faker library in following way.

import faker

fake_data = Faker()

female_name = fake_data.name_female()
print("Female Name : ", female_name)

male_name = fake_data.name_male()
print("Male Name : ", male_name)
  • name_female() method is used to generate female names.
  • name_male() method is used to generate male names.

Output

Generating First And Last Name

Working with faker library is very easy because it provides very easy to remember methods to generate fake data. Some time we require first and last name. We can also generate first and last name using faker library.

first_name() method is used to generate first name and last_name() method is used to generate last name. So let’s implement them to get first and last name. Write and run following code to generate first and last name.

import faker

fake_data = Faker()

f_name = fake_data.first_name()
print("First Name : ", f_name)

l_name = fake_data.last_name()
print("Last Name : ", l_name)

Output

Here is its output.

We can also generate first and last name of male and female. Here is the code.

import faker

fake_data = Faker("en_IN")

#Female first name 
female_name = fake_data.first_name_female()
print("Female First Name : ", female_name)

#Male first name 
male_name = fake_data.first_name_male()
print("Male First Name : ", male_name)

#Female last name 
female_l_name = fake_data.last_name_female()
print("Female Last Name : ", female_l_name)

#Male last name 
male_l_name = fake_data.last_name_male()
print("Male Last Name : ", male_l_name)

Output

Generating Name’s Prefix and Suffix

Do you also want to generate name’s prefix and suffix? Here is the code to do that.

import faker

fake_data = Faker()

name_prefix = fake_data.prefix()
print("Prefix Name : ", name_prefix)

name_suffix = fake_data.suffix()
print("Suffix Name : ", name_suffix)
  • prefix() method is used to generate prefix.
  • suffix() method is used to generate suffix.

So guys it was all about Python Fake Name Generator tutorial. You have learned generating fake names in different ways using python faker library. I hope you found this tutorial easy and helpful. But still if you have any doubt then feel free to comment your queries. In upcoming tutorials, you will learn more faker tutorials like generating emails, credit cards, addresses phone numbers and many more. So stay tuned with Dggul AI Tutorial for more faker tutorials. THANKS

Related Articles… 

Leave a Comment