This function generates a random string consisting of uppercase and lowercase letters, numbers, length delimiter, no_of_blocks to indicate the format of the string
For example: len_sep = 4, no_of_blocks = 4 will generate the following pattern,
F4nQ-Vh5z-JKEC-WhuS
Where, the length delimiter will add a "-" after 4 characters
XXXX-
none of the blocks will generate the next character set as a string
XXXX - XXXX - XXXX - XXXX
if you need one random string, just leave the no_of_blocks variable equal to 1 and len_sep to indicate the length of the random string.
For example: len_sep = 10, no_of_blocks = 1, will generate the following pattern, i.e. random string 10 in length
F01xgCdoDU
import random as r def generate_random_string(len_sep, no_of_blocks): random_string = '' random_str_seq = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" for i in range(0,len_sep*no_of_blocks): if i % len_sep == 0 and i != 0: random_string += '-' random_string += str(random_str_seq[r.randint(0, len(random_str_seq) - 1)]) return random_string
Manoj Selvin Feb 24 '18 at 17:28 2018-02-24 17:28
source share