Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
July 21, 2022 06:52 am GMT

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'....]

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 alphabetscharacters = list(string.ascii_letters)# adding numbers from 0 to 9characters.extend(list(x for x in range(0,10)))# adding all the special characterscharacters.extend(list(string.punctuation))

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, '!', '"', '#', '$', '%', '&', "'", '(', ')', '*', '+', ',', '-', '.', '/', ':', ';', '<', '=', '>', '?', '@', '[', '\\', ']', '^', '_', '`', '{', '|', '}', '~']

Complete Code for creating random password generator using Python.

import stringimport randomnum = int(input('Enter the length of password : '))# getting all the alphabetscharacters = list(string.ascii_letters)# adding numbers from 0 to 9characters.extend(list(x for x in range(0,10)))# adding all the special characterscharacters.extend(list(string.punctuation))password = ''for i in range(num):    password = password + str(random.choice(characters));print(password)

Thanks for Reading
Like and Follow.


Original Link: https://dev.to/vamsitupakula_/random-password-generator-easiest-way-python--5ep6

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To