Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)

My inquiry is: How to produce an arbitrary random Password with a length that fluctuates from (8 to 12 characters) each time created. 

Look at my code:

import string

import random

def randompassword():

    chars=string.ascii_uppercase + string.ascii_lowercase + string.digits

    size=8 

    return ''.join(random.choice(chars) for x in range(size,12))

print(randompassword())

1 Answer

0 votes
by (26.4k points)

Here, size=8; range(size,12) continuously returns an array [8,9,10,11], so you generally get a password of length 4. All things considered, decide the size of this specific password utilizing randint early:

import string

import random

def randompassword():

  chars = string.ascii_uppercase + string.ascii_lowercase + string.digits

  size = random.randint(8, 12)

  return ''.join(random.choice(chars) for x in range(size))

Join the python course fast, to learn python concepts in detail and get certified.

Browse Categories

...