You can download this code by clicking the button below.
This code is now available for download.
Generate a random string of specified length using letters and digits.
Technology Stack : Built-in libraries: string, random
Code Type : Function
Code Difficulty : Intermediate
import random
import string
import datetime
import json
import hashlib
import re
import math
import os
import sys
def generate_random_string(length):
"""
Generate a random string of specified length using letters and digits.
"""
if not isinstance(length, int) or length <= 0:
raise ValueError("Length must be a positive integer")
characters = string.ascii_letters + string.digits
return ''.join(random.choice(characters) for i in range(length))