37
loading...
This website collects cookies to deliver better user experience
python
website, Python is a programming language that lets you work quickly and integrate systems more effectively. interpreted
, object-oriented
, high-level
programming language with dynamic semantics. list
, shuffle
them randomly and join
them to a string.random
.import random
import string
string.printable
.def split(word):
return [char for char in word]
split
helper function will split string.printable
into a list of all the individual charters of ascii.all_strings = split(string.printable)
all_strings
digits = all_strings[0:10]
lowerCase = all_strings[10:36]
upperCase = all_strings[36:62]
syms = all_strings[62:94]
all_strings
list to extract from and to the required index of each type of character.Indexing in python starts from 0.
# create helper functions to generate two random characters of each
def randomLower(lowerCase):
return random.choices(lowerCase, k=2)
def randomUpper(upperCase):
return random.choices(upperCase, k=2)
def randomDigits(digits):
return random.choices(digits, k=2)
def randomSyms(syms):
return random.choices(syms, k=2)
choices()
method returns a list with the randomly selected element from the specified sequencedef twoRandom(coll):
return random.choices(coll, k=2)
temp = twoRandom(syms)+twoRandom(digits)+twoRandom(lowerCase) +twoRandom(upperCase)
random.shuffle(temp)
passwd = "".join(temp)
concatenate
the generated string in one list and use the shuffle()
method to shuffle the list.join()
method is used to join items in a list by a delimiter.print(passwd)