How to generate random names (first and last names) with python ?

Published: March 23, 2021

Tags: Python;

DMCA.com Protection Status

In python, I used to enter random names manually (like John Doe Or Jana Doe) to create for example fake database and for testing. However to generate random names a better solution is to use for example the module "names" created by Trey Hunner:

Install the module names

To install names with pip

pip install names

Install the module names with anaconda

If you work with anaconda just do:

conda create -n my_env

source activate my_env

conda install pip

pip install names

Create random names with python

Then to create random names just do:

import names

for i in range(10):
    print(names.get_full_name())

returns for examples:

April Reiter
Emory Miller
David Ballin
Alice Trotter
Virginia Rios
Thomas Wheeler
James Harrell
Nicolas White
Mary Flanagan
Velda Grubb

Create random male names

for i in range(10):
    rand_name = names.get_full_name(gender='male')
    print(rand_name)

returns

Arthur Manning
Victor Ishee
Roland Chambless
Fred Shawler
Nicholas North
James Michaud
Mitchell Dorsey
Willie Porras
Antonio Green
Joe Sherman

Create random female names

for i in range(10):
    rand_name = names.get_full_name(gender='female')
    print(rand_name)

returns

Lorri Boles
Carin Hodge
Judith Mcdaniel
Elaine Jones
Terri Tanguay
Caroline Crowley
Edith Jones
Katlyn Bellamy
Jeannie Mayberry
Marge Swaim

Create random male first names

for i in range(10):
        rand_name = names.get_first_name(gender='male')
        print(rand_name)

returns

George
Shawn
Robert
Steven
William
James
Christopher
James
Michael
Donovan

Create random female first names

for i in range(10):
    rand_name = names.get_first_name(gender='female')
    print(rand_name)

returns

Jessica
Stephanie
Ann
Emma
Heather
Anna
Kelli
Pauline
Tanya
Kathy

Create random last names

for i in range(10):
    rand_name = names.get_last_name()
    print(rand_name)

returns

Sanders
Meyer
Ingram
Straight
Caldwell
Cox
Hudson
Basso
Millhouse
Rivett

References