What is python?
According to the official python
website, Python is a programming language that lets you work quickly and integrate systems more effectively.
A much more specific meaning would be: its an interpreted
, object-oriented
, high-level
programming language with dynamic semantics.
In this article we will code a python program that generates a random 8 characters long password.
A password is a secret phrase that must be used to allow access to a computer system or service.
To ensure each password generated is strong, it must contain each of the following:
- 2 random lower case letters
- 2 random upper case letters
- 2 random numeric characters
- 2 random symbols
Breaking down the process ?
The process involves generating two characters of each type (letters, numbers and symbols), combine them in a list
, shuffle
them randomly and join
them to a string.
List: A list is a python data type used to store multiple items in a single variable.
To begin writing the program, its important we understand the requirements.
We need to make the password as unique and random as possible. For this we need to import random
.
Module: A module is a Python file that's intended to be imported into scripts
library : Python Libraries are a set of useful functions that eliminate the need for writing codes from scratch.
Let's code, shall we?
Importing the required modules.
import random
import string
String module has a pre-initialized string used as string constant, string.printable
.
Create a helper function for splitting a string.
def split(word):
return [char for char in word]
The split
helper function will split string.printable
into a list of all the individual charters of ascii.
all_strings = split(string.printable)
Split returns a list containing all of the ascii characters, which we store in a variable all_strings
Extract new lists for each type of ascii characters
digits = all_strings[0:10]
lowerCase = all_strings[10:36]
upperCase = all_strings[36:62]
syms = all_strings[62:94]
We use the indexing of each of the characters in the all_strings
list to extract from and to the required index of each type of character.
Indexing in python starts from 0.
Create helpers functions to generate random characters of each type
# 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)
random.choices: The choices()
method returns a list with the randomly selected element from the specified sequence
To refactor the code further:
def twoRandom(coll):
return random.choices(coll, k=2)
The function will return two random items from a list passed to in.
Concatenate the random strings in one list
temp = twoRandom(syms)+twoRandom(digits)+twoRandom(lowerCase) +twoRandom(upperCase)
random.shuffle(temp)
passwd = "".join(temp)
We concatenate
the generated string in one list and use the shuffle()
method to shuffle the list.
The join()
method is used to join items in a list by a delimiter.
Now we can print out the result
print(passwd)
Top comments (0)