Random String Generator Function

  • Share this:

Code introduction


This code defines a function to generate a random string of specified length. It uses the random and string modules. The first argument is the length of the string, and the second argument is the character set, defaulting to both uppercase and lowercase letters.


Technology Stack : random, string

Code Type : Function

Code Difficulty :


                
                    
import random
import string
import time
import datetime
import os
import sys
import math

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

def get_time_difference(start_time):
    return datetime.datetime.now() - start_time

def create_directory(path):
    if not os.path.exists(path):
        os.makedirs(path)

def print_system_info():
    print("Python version:", sys.version)
    print("Platform:", sys.platform)
    print("Machine:", sys_machine)

def calculate_square_root(value):
    return math.sqrt(value)                
              
Tags: