Random String Generator with Python

  • Share this:

Code introduction


This function generates a random string of specified length using the built-in random and string modules.


Technology Stack : random, string

Code Type : Custom function

Code Difficulty : Intermediate


                
                    
import random
import string
import math
import datetime
import os

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

def calculate_circle_area(radius):
    return math.pi * (radius ** 2)

def log_timestamp(message):
    timestamp = datetime.datetime.now()
    return f"{timestamp}: {message}"

def read_file(file_path):
    if os.path.exists(file_path):
        with open(file_path, 'r') as file:
            return file.read()
    else:
        return "File not found."

def filter_even_numbers(numbers):
    return [num for num in numbers if num % 2 == 0]

def json_serializable_data(data):
    import json
    return json.dumps(data)                
              
Tags: