Random Password Generator

  • Share this:

Code introduction


This function is used to generate a random password of a specified length, including uppercase and lowercase letters, numbers, and special characters.


Technology Stack : string, random

Code Type : Function

Code Difficulty : Intermediate


                
                    
import datetime
import random
import string
import re
import math
import os
import sys

def generate_random_password(length=8):
    if length < 8:
        raise ValueError("Password length must be at least 8 characters.")
    
    characters = string.ascii_letters + string.digits + string.punctuation
    password = ''.join(random.choice(characters) for i in range(length))
    return password                
              
Tags: