Password Generator using Python|Psword Generator using Random in python
Random 5 letter password generator using Random Librery ,its a simple code,consits of random Librery ,list and set of strings joind togethon to obtain random passwords after the code gets runs,There is no need of installing separate package,Random is a preinstalled Librery.
This Python code generates a random password consisting of five uppercase letters, shuffles their order, and then prints the result. Here's a breakdown of how it works:
1. Importing the random
Module
pythonimport random
The code begins by importing the random
module, which provides various functions to generate random numbers, shuffle sequences, and more.
2. Defining the shuffle
Function
pythondef shuffle(string):
tempList = list(string)
random.shuffle(tempList)
return ''.join(tempList)
This function takes a string as input, converts it into a list of characters (tempList
), shuffles the list randomly using random.shuffle()
, and then joins the shuffled list back into a string to return it.
3. Generating Random Uppercase Letters
pythonuppercaseLetter1 = chr(random.randint(65, 90))
uppercaseLetter2 = chr(random.randint(65, 90))
uppercaseLetter3 = chr(random.randint(65, 90))
uppercaseLetter4 = chr(random.randint(65, 90))
uppercaseLetter5 = chr(random.randint(65, 90))
random.randint(65, 90)
generates a random integer between 65 and 90 (inclusive).- The
chr()
function converts this integer to its corresponding ASCII character. Since 65 to 90 are the ASCII values for uppercase letters 'A' to 'Z', each of these five variables (uppercaseLetter1
touppercaseLetter5
) will hold a random uppercase letter.
4. Concatenating the Letters to Form a Password
pythonpassword = uppercaseLetter1 + uppercaseLetter2 + uppercaseLetter3 + uppercaseLetter4 + uppercaseLetter5
Here, the five randomly generated uppercase letters are concatenated into a single string called password
.
5. Shuffling the Password
pythonpassword = shuffle(password)
The shuffle
function is called with the password
string as its argument. The function returns a shuffled version of the password, which is then stored back in the password
variable.
6. Printing the Password
pythonprint(password)
Finally, the shuffled password is printed to the console.
Summary
This code randomly generates a 5-letter uppercase password, shuffles the order of the letters, and prints the result. The randomness ensures that each time you run the code, you'll likely get a different password.
MAIN CODE
import random def shuffle(string): tempList = list(string) random.shuffle(tempList) return ''.join(tempList) #Main program starts here uppercaseLetter1=chr(random.randint(65,90)) uppercaseLetter2=chr(random.randint(65,90)) uppercaseLetter3=chr(random.randint(65,90)) uppercaseLetter4=chr(random.randint(65,90)) uppercaseLetter5=chr(random.randint(65,90)) password = uppercaseLetter1 + uppercaseLetter2 + uppercaseLetter3 + uppercaseLetter4 + uppercaseLetter5 password = shuffle(password) print(password)
0 Comments