A series of numbers and letters that have no specific pattern is considered as a Random String. It also known as Random Character String. Basically It used to generate a unique set of strings with the pupose of data testing. In Python, random module used to generate the random strings.
Generating a random string
secrets.choice() functions from random module used to generate a secure string. By using python string, The
random.choice() function generate the sequence of characters and digits that can repeat the string in any order. There are some methods that commonly used to specify the type of generated string. Such as:
string.ascii_letters used for both uppercase and lowercase characters.
string._ascii_uppercase used for only a string in uppercase characters.
string._ascii_lowercase used for only a string in lowercase characters.
string.digits used for a string with numeric characters.
string.punctuation used for a string with punctuation characters.
result = ''.join((random.choice(string.ascii_lowercase) for x in range(length)))
print(" Random string generated in Lowercase: ", result)
result1 = ''.join((random.choice(string.ascii_uppercase) for x in range(length)))
print(" Random string generated in Uppercase: ", result1)
Random string generated in Lowercase: wnzzuhkeao
Random string generated in Uppercase: MNNSXFMOSR
It should be noted that, the
random.choice() method may gives repetation of a single character for multiple times. To avoid repetation of same character, just use the