Generate Random Strings and Passwords in Python


If password contains characters, digits and special symbols are considered as a strong password.

For example, if we want to generate a random Password like below:-

ab2@5efd#$
j*&k%m&32
T871t@h*ki%


Python Program to Generate a random password string with Special characters, letters, and digits


In this Python program we are going to use string.ascii_letters , string.digits and string.punctuation along with random.choice function.


import random
import string
def random_String(string_Length=10):
    """Generating a random string which contains letters, digits and special characters """
    password_characters = string.ascii_letters + string.digits + string.punctuation
    return ''.join(random.choice(password_characters) for j in range(string_Length))
print("Generating Random String password containg letters, digits and special characters ")
print ("First Random String ", random_String() )
print ("Second Random String", random_String(13) )
print ("Third Random String", random_String(15) )
print ("fourth Random String", random_String(20) )

Output:-

Generating Random String password containg letters, digits and special characters
First Random String  F;q;ps<#qN
Second Random String PxgsgjOQK/36H
Third Random String 4F%!.QEsm&lnr&w
fourth Random String uF%($FJ,NVAsWvwq\D;,

 


Python Program to generate a random password String which contains lowercase character,uppercase character, digits and special character in Python.


import random
import string
def random_Password():
    """Generating a random password contains uppercase,lowercase,digit and symbol """
    random_Source = string.ascii_letters + string.digits + string.punctuation
    Random_password = random.choice(string.ascii_lowercase)
    Random_password += random.choice(string.ascii_uppercase)
    Random_password += random.choice(string.digits)
    Random_password += random.choice(string.punctuation)
    for j in range(6):
        Random_password += random.choice(random_Source)
    password_List = list(Random_password)
    random.SystemRandom().shuffle(password_List)
    password = ''.join(password_List)
    return password
print ("First Random Password is ", random_Password())
print ("Second Random Password is ", random_Password())
print ("Third Random Password is ", random_Password())
print ("Fourth Random Password is ", random_Password())

Output:-

First Random Password is  %/[5[JOt}j
Second Random Password is  tdwBMNV[6L
Third Random Password is  XHSNnG(^6<
Fourth Random Password is  ^tv\@tH

If password contains characters, digits and special symbols are considered as a strong password.

For example, if we want to generate a random Password like below:-

ab2@5efd#$
j*&k%m&32
T871t@h*ki%


Python Program to Generate a random password string with Special characters, letters, and digits


In this Python program we are going to use string.ascii_letters , string.digits and string.punctuation along with random.choice function.


import random
import string
def random_String(string_Length=10):
    """Generating a random string which contains letters, digits and special characters """
    password_characters = string.ascii_letters + string.digits + string.punctuation
    return ''.join(random.choice(password_characters) for j in range(string_Length))
print("Generating Random String password containg letters, digits and special characters ")
print ("First Random String ", random_String() )
print ("Second Random String", random_String(13) )
print ("Third Random String", random_String(15) )
print ("fourth Random String", random_String(20) )

Output:-

Generating Random String password containg letters, digits and special characters
First Random String  F;q;ps<#qN
Second Random String PxgsgjOQK/36H
Third Random String 4F%!.QEsm&lnr&w
fourth Random String uF%($FJ,NVAsWvwq\D;,

 


Python Program to generate a random password String which contains lowercase character,uppercase character, digits and special character in Python.


import random
import string
def random_Password():
    """Generating a random password contains uppercase,lowercase,digit and symbol """
    random_Source = string.ascii_letters + string.digits + string.punctuation
    Random_password = random.choice(string.ascii_lowercase)
    Random_password += random.choice(string.ascii_uppercase)
    Random_password += random.choice(string.digits)
    Random_password += random.choice(string.punctuation)
    for j in range(6):
        Random_password += random.choice(random_Source)
    password_List = list(Random_password)
    random.SystemRandom().shuffle(password_List)
    password = ''.join(password_List)
    return password
print ("First Random Password is ", random_Password())
print ("Second Random Password is ", random_Password())
print ("Third Random Password is ", random_Password())
print ("Fourth Random Password is ", random_Password())

Output:-

1

 


 

Leave a Reply

Your email address will not be published. Required fields are marked *