Creating a Simple Project Using Python Library Functions
In this simple project, we explore Python library functions like string
and random
to manipulate character sets.
Available Constants in the string
Module:
string.ascii_letters
: Concatenation of lowercase and uppercase letters.string.ascii_lowercase
: Lowercase letters from 'a' to 'z'.string.ascii_uppercase
: Uppercase letters from 'A' to 'Z'.string.digits
: The string '0123456789'.string.hexdigits
: Hexadecimal digits covering '0123456789abcdefABCDEF'.string.octdigits
: Octal digits ranging from '0' to '7'.string.punctuation
: ASCII characters considered as punctuation.
Python Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import math | |
alpha = "abcdefghijklmnopqrstuvwxyz" | |
num = "0123456789" | |
special = "@#$%&*" | |
# pass_len=random.randint(8,13) #without User INput | |
pass_len = int(input("Enter Password Length")) | |
# length of password by 50-30-20 formula | |
alpha_len = pass_len//2 | |
num_len = math.ceil(pass_len*30/100) | |
special_len = pass_len-(alpha_len+num_len) | |
password = [] | |
def generate_pass(length, array, is_alpha=False): | |
for i in range(length): | |
index = random.randint(0, len(array) - 1) | |
character = array[index] | |
if is_alpha: | |
case = random.randint(0, 1) | |
if case == 1: | |
character = character.upper() | |
password.append(character) | |
# alpha password | |
generate_pass(alpha_len, alpha, True) | |
# numeric password | |
generate_pass(num_len, num) | |
# special Character password | |
generate_pass(special_len, special) | |
# suffle the generated password list | |
random.shuffle(password) | |
# convert List To string | |
gen_password = "" | |
for i in password: | |
gen_password = gen_password + str(i) | |
print(gen_password) |