Random Password Generator

  • Share this:

Code introduction


This function generates a random password of specified length, including uppercase and lowercase letters, digits, and special characters.


Technology Stack : random, string

Code Type : Generate random password

Code Difficulty : Intermediate


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

def generate_random_password(length=8):
    if not isinstance(length, int) or length < 1:
        raise ValueError("Password length must be a positive integer.")
    
    characters = string.ascii_letters + string.digits + string.punctuation
    return ''.join(random.choice(characters) for i in range(length))                
              
Tags: