You can download this code by clicking the button below.
This code is now available for download.
Generates a random string of specified length
Technology Stack : random, string
Code Type : Function
Code Difficulty : Intermediate
import random
import string
import math
import datetime
def generate_random_string(length, chars=string.ascii_letters + string.digits):
return ''.join(random.choice(chars) for _ in range(length))
def calculate_circle_area(radius):
return math.pi * (radius ** 2)
def time_since_now(days):
return datetime.datetime.now() - datetime.timedelta(days=days)
def reverse_string(input_string):
return input_string[::-1]
def filter_even_numbers(numbers):
return [number for number in numbers if number % 2 == 0]
def sort_words(words):
return sorted(words, key=lambda s: s.lower())
def encrypt_message(message, shift):
encrypted = ''
for char in message:
if char.isalpha():
shifted = ord(char) + shift
if char.islower():
if shifted > ord('z'):
shifted -= 26
elif char.isupper():
if shifted > ord('Z'):
shifted -= 26
encrypted += chr(shifted)
else:
encrypted += char
return encrypted
def find_longest_word(text):
words = text.split()
return max(words, key=len)
def json_to_dict(json_string):
import json
return json.loads(json_string)
def dict_to_json(dictionary):
import json
return json.dumps(dictionary)