How to generate random password in python [duplicate]

75
January 13, 2021, at 00:10 AM
This question already has answers here:
Generate password in python (9 answers)
Closed 2 hours ago.

I want to use python3 to generate random password to encrypt my files, The random password created should have following restriction

  1. Minimum length should be 12
  2. Must contain one uppercase letter.
  3. Must contain one lowercase letter.
  4. Must contain one digit.
  5. Must contain one special character.

Actually I don't know much about generating random things in python with restrictions so I don't have any code to show here.

Answer 1

You can choose a character from a list with the python random library. Here is an example:

import string
import random
symbols = ['*', '%', '£'] # Can add more
password = ""
for _ in range(9):
    password += random.choice(string.ascii_lowercase)
password += random.choice(string.ascii_uppercase)
password += random.choice(string.digits)
password += random.choice(symbols)
print(password)
Answer 2

I'm relatively new to python and stackoverflow but here's my shot at your problem:

import string
import random

def password_generator(length):
    """ Function that generates a password given a length """
    uppercase_loc = random.randint(1,4)  # random location of lowercase
    symbol_loc = random.randint(5, 6)  # random location of symbols
    lowercase_loc = random.randint(7,12)  # random location of uppercase
    password = ''  # empty string for password
    pool = string.ascii_letters + string.punctuation  # the selection of characters used
    for i in range(length):
        if i == uppercase_loc:   # this is to ensure there is at least one uppercase
            password += random.choice(string.ascii_uppercase)
        elif i == lowercase_loc:  # this is to ensure there is at least one uppercase
            password += random.choice(string.ascii_lowercase)
        elif i == symbol_loc:  # this is to ensure there is at least one symbol
            password += random.choice(string.punctuation)
        else:  # adds a random character from pool
            password += random.choice(pool)
    return password  # returns the string
print(password_generator(12))

I imported two modules, one being 'string', which gives me access to strings with all the characters I need. The other being 'random', which allows me to generate random numbers and pick a random character from a string.

Using random.randint(a, b) I could generate random locations for uppercase, lowercase, and punctuation characters, to ensure that there would be at least one of each.

The only modification I made is that I made it that you can generate a password of any length as long as you input said length into the function.

Here is an example of an output: L"mJ{~xcL[%M

Answer 3

Below code doing exactly what I asked for but I am marking @OliverF code as the solution to this problem as his code is also working fine and posted before of my own answer.

#!/usr/bin/env python3
#import the necessary modules!
import random
import string
length = random.randint(12,25)
lower = string.ascii_lowercase
upper = string.ascii_uppercase
num = string.digits
symbols = string.punctuation
    
all = lower + upper + num + symbols
temp = random.sample(all,length)
password = "".join(temp)
print(password)
Answer 4

you can generate random bits, convert them to an integer, clamp them so it doesn't pick weird characters and convert them to char

 import random
 
  passwrd = ''
  length = 12
  for _ in range(length):
      bits = random.getrandbits(8)
      num = (int('{0:b}'.format(bits),2) + 33) % 127
      passwrd+= chr(num)

then you can check if your conditions are satisfied.

READ ALSO
Post Image data in android studio to a server url not working on android 4.1.2

Post Image data in android studio to a server url not working on android 4.1.2

I am taking a screenshot on my android app using this code:

75
Android studio Build failed with NDK

Android studio Build failed with NDK

found error while build the project with NDK

79
How to make an extensive Website with 100s pf pages like w3school? [closed]

How to make an extensive Website with 100s pf pages like w3school? [closed]

Want to improve this question? Add details and clarify the problem by editing this post

16