Do you want to perform cool things like generating random strings using Python? This tutorial is really useful for all python beginners and developers. Are you bad at remembering the password or want to generate the password? then creating a random string of characters and numbers in python is the perfect way to understand. Successfully learn how to generate random strings or passwords using python by following the below modules carefully.

Python Random String Generator

Prevailing Python Code Snippets gives this very helpful snippet for producing random strings as a password generator that can simply be utilized in various projects that run on Python. In the snippet, the password generator performs a random string with a min of 8 characters and a max of 12, which will hold letters, numbers, and punctuation. When the string has been generated, it’s printed. Then your users (or you) are free to use it for any of their password needs.

We’ve modified the snippet to our own liking and given it below. Check it out and feel free to use it, alter it, or simply customize it.

import string
from random import *
min_char = 8
max_char = 12
allchar = string.ascii_letters + string.punctuation + string.digits
password = "".join(choice(allchar) for x in range(randint(min_char, max_char)))
print "This is your password : ",password

See More:

How I Generated a random string of given length using python?

By importing the two python modules, you can easily generate a random string of a given length. Let’s see them:

  1. string
  2. random

The string module

It includes many string constant that holds the ASCII characters of all cases. The string module comprises separate constants for lowercase, uppercase letters, digits, and special characters.

Random module

The random module is useful to perform the random generations for us.

Function Definition

def createRandomString(length):
    letters = string.ascii_lowercase
    randomString = ''.join(random.choice(letters) for i in range(length))
    return randomString
  • string.ascii_lowercase this gives us all the lowercase letters such as ‘abcdefghijklmnopqrstuvwxyz’
  • Perform for loop length times and use random.choice() to choose a single character from the string constant. At the end of each iteration, add it to the string variable with the help of a join function. The random.choice() function is utilized to select a single item from any sequence.

Example of Generating Random Password or String Using Lamda Expression

import random
import string

get_random_string = lambda length, \
                  all_chars = string.ascii_letters + string.digits + string.punctuation: \
                  "".join([list(set(all_chars))[random.randint(0, len(list(set(all_chars)))-1)] \
                  for _ in range(length)])

print("Random string-1:", get_random_string(10))
print("Random string-2:", get_random_string(15))
print("Random string-3:", get_random_string(20))

Output:

The output will remain altered from one platform to other.

Example of Generating Random Password or String with Alphabets of Uppercase or Lowercase, Digits, and Punctuation of Specific Length

import random
import string

# uchars = Uppercase charaters
# lchars =  Lowercase charaters
# dchars = Digits
# schars = Punctuation or Special Charaters

def get_random_string(uchars = 3, lchars = 3, dchars = 2, schars = 2):
    # Generates a 10 characters long random string
    # with 3 upper case, 3 lowe case, 2 digits and 2 special characters

    str_uchars, str_lchars, str_dchars, str_schars = '', '', '', ''

    for i in range(uchars):
        str_uchars += random.SystemRandom().choice(string.ascii_uppercase)

    for i in range(lchars):
        str_uchars += random.SystemRandom().choice(string.ascii_lowercase)

    for i in range(dchars):
        str_uchars += random.SystemRandom().choice(string.digits)

    for i in range(schars):
        str_uchars += random.SystemRandom().choice(string.punctuation)

    random_str = str_uchars + str_lchars + str_dchars + str_schars
    random_str = ''.join(random.sample(random_str, len(random_str)))
    return random_str


print('Your Random String-1:', get_random_string())

# generates five uppercase, 5 lowercase, 3 digits and 2 punctuation
print('Your Random String-2:', get_random_string(5, 5, 3, 2))

Output:

Your Random String-1: -oUfH78eU(
Your Random String-2: LnuDsJ]61qHpD4~

Leave a Reply

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