Random String Generator

  • Share this:

Code introduction


This function generates a random string of specified length, consisting of both uppercase and lowercase letters.


Technology Stack : random, string

Code Type : Function

Code Difficulty : Intermediate


                
                    
import random
import math
import string
import datetime

def generate_random_string(length):
    """
    生成一个指定长度的随机字符串。
    """
    if not isinstance(length, int) or length <= 0:
        raise ValueError("Length must be a positive integer")

    letters = string.ascii_letters
    return ''.join(random.choice(letters) for i in range(length))                
              
Tags: