You can download this code by clicking the button below.
This code is now available for download.
The function generates a random string of a specified length, with an optional prefix at the beginning of the string.
Technology Stack : Built-in libraries: string, random
Code Type : Function
Code Difficulty : Intermediate
import math
import random
import string
import datetime
def generate_random_string(length, prefix=''):
if not isinstance(length, int) or length <= 0:
raise ValueError("Length must be a positive integer")
letters = string.ascii_letters
return prefix + ''.join(random.choice(letters) for i in range(length))