Python Generate Random String Of Specific Length

Random string is used widely in password, uuid, captcha text, verification number etc. Python string, random and uuid module provide method to generate random string with digit or alphabet content or digital and alphabet mixed content. This article will show you some examples.

1. Generate Random String Steps.

  1. Create a base character list which contains all the characters or digits used to create the random string. The python string module digitsascii_letters property contains number and lower and upper characters.
  2. Use random.choice method to select one character from the base character list randomly.
  3. Use the random selected character to build a string.

2. Random String Generation Example.

'''
Created on Aug 24, 2018
@author: zhaosong
'''

import string

import random

import uuid

# Common function to generate a string_size length random string based on the first parameter.
def generate_random_string(base_string_character, string_size=10):

    ret_str = ''

    # Select random character for string_size times.

    for i in range(string_size):

        # Random select one character from the base character string.

        character = random.choice(base_string_character)

        # Append the selected character to the return string.

        ret_str += character

    return ret_str    

# Generate random digit content string only.

def generate_random_digit(str_len=10):

    ret = generate_random_string(string.digits, str_len)

    print("Random digit : " + ret)

    return ret

# Generate random alphabet content string.

def generate_random_alphabet(str_len=10):

    ret = generate_random_string(string.ascii_letters, str_len)

    print("Random alphabet : " + ret)

    return ret

# Generate random alphabet and digits content string.

def generate_random_alphabet_digit(str_len=10):

    ret = generate_random_string(string.digits + string.ascii_letters, str_len)

    print("Random alphabet and digit: " + ret)

    return ret

# Generate random string with specified digit count and alphabet character count.

def generate_random_alphabet_digit_with_special_number(digit_number=5, alphabet_number=5):

    # Generate specified length digit string.
    digit_str = generate_random_digit(digit_number)


    # Generate specified length alphabet string.
    alphabet_str = generate_random_alphabet(alphabet_number)

  
    # Add above two random string.
    tmp_str = digit_str + alphabet_str  

    # Convert above string to list.
    tmp_str_list = list(tmp_str)
    
    tmp_str_len = len(tmp_str)

    # Scatter characters order in the string list and return a new ordered string list.
    ret = random.sample(tmp_str_list, tmp_str_len)

    # Convert string list back to a string.
    ret = str(ret)
    ret = ret.strip('[').strip(']')
    ret = ret.replace(",", "").replace("'", "").replace(" ", "")

    print("Random special length alphabet digit : " + ret)    

    return ret

# Use python uuid module to generate a uuid.
def generate_random_uuid():

    # Create uuid.

    ret = uuid.uuid1()

    # Convert uuid to string.

    ret = str(ret)

    print("uuid : " + ret)

    return ret

if __name__ == '__main__':

   generate_random_digit()

   generate_random_alphabet()

   generate_random_alphabet_digit()

   generate_random_alphabet_digit_with_special_number(3,6)

   generate_random_uuid()

Below is the example output.

Random digit : 4483125781

Random alphabet : kegGYGCQxM

Random alphabet and digit: mlL01PbNTj

Random digit : 367

Random alphabet : CjyyHp

Random special length alphabet digit : 3j7yy6pCH

uuid : e6240cc8-a775-11e8-bd9e-10ddb19bb0b9

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.