DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 963,673 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Vamsi Krishna
Vamsi Krishna

Posted on

Random Password generator - Easiest way { Python }

In this post I will help you build a random password generator in the most easiest and efficient way.

How can we build this πŸ€”?

  1. First thing we will do is to ask the user what is the length of the string
  2. We will get random values from a list containing all the alphabets(both uppercase and lowercase), numbers and special characters like #,@,#...

⚠️⚠️⚠️ Mistake we need to avoid ⚠️⚠️⚠️

Most of the people generally makes this mistake while doing this project. they will create a list and they will start writing all the characters, numbers manually... like below code πŸ‘‡πŸ‘‡

characters = ['a','b','c','d',......,'A','B','C'....]
Enter fullscreen mode Exit fullscreen mode

This will definitely not make you a good programmer.

Instead do this βœ…βœ…

There is a module named string in python which will give you all the alphabets and special characters.

Create a list which contain all the alphabets, numbers and special characters as shown below.

import string

# getting all the alphabets
characters = list(string.ascii_letters)

# adding numbers from 0 to 9
characters.extend(list(x for x in range(0,10)))

# adding all the special characters
characters.extend(list(string.punctuation))
Enter fullscreen mode Exit fullscreen mode

characters list will look as shown below.

['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, '!', '"', '#', '$', '%', '&', "'", '(', ')', '*', '+', ',', '-', '.', '/', ':', ';', '<', '=', '>', '?', '@', '[', '\\', ']', '^', '_', '`', '{', '|', '}', '~']
Enter fullscreen mode Exit fullscreen mode

πŸš€ Complete Code for creating random password generator using Python.

import string
import random

num = int(input('Enter the length of password : '))

# getting all the alphabets
characters = list(string.ascii_letters)

# adding numbers from 0 to 9
characters.extend(list(x for x in range(0,10)))

# adding all the special characters
characters.extend(list(string.punctuation))

password = ''

for i in range(num):
    password = password + str(random.choice(characters));

print(password)
Enter fullscreen mode Exit fullscreen mode

Thanks for Reading πŸ’–πŸ’–
Like and Follow.

Latest comments (6)

Collapse
 
vamsitupakula_ profile image
Vamsi Krishna Author

Hey, Lukasz,
This is just a beginner project which help someone to understand string module and random module. it's not like a real password generating project.

Thank You. :-)

Collapse
 
lksmrqrdt profile image
Lukas Marquardt • Edited on

Please do not use random.choice() for this kind of task.
This function doesn't generate cryptographically secure randomness and is not suited for this task.

The documentation mentions this:

Warning: The pseudo-random generators of this module should not be used for security purposes. For security or cryptographic uses, see the secrets module.

Collapse
 
lukeshiru profile image
Luke Shiru

I find this kinda funny because every time I see a "create a random password generator" post in JavaScript, they have the same issue in which they use Math.random() which isn't meant to be used for cryptographic security.

Collapse
 
krishnaagarwal profile image
Krishna Agarwal

Thanks a lot.

Collapse
 
vamsitupakula_ profile image
Vamsi Krishna Author

Welcome πŸ™Œ

🌚 Friends don't let friends browse without dark mode.

Sorry, it's true.