1

i got a program from one of the site to make password generator.

import string
from random import *
characters = string.ascii_letters + string.punctuation  + string.digits
password =  "".join(choice(characters) for x in range(randint(8, 16)))
print password

can someone explain what would be stored in "password" variable? would it be a value with a combination of upper,lower case,punctuation and digits with any length btwn 8 and 16?

is there any easier way to make password generator using python program?

8
  • what exactly the double quotes and dot does? Commented Aug 2, 2015 at 18:55
  • stackoverflow.com/questions/1876191/explain-python-join# Commented Aug 2, 2015 at 18:56
  • 2
    What happened when you ran the code? Commented Aug 2, 2015 at 18:57
  • it is generating the random passwords. Commented Aug 2, 2015 at 18:59
  • Python ha fairly good documentation if you ever want to check what a function does, docs.python.org/2/library/stdtypes.html#str.join Commented Aug 2, 2015 at 18:59

1 Answer 1

3

If you print characters you will get a list of characters and symbols.

abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~0123456789

The next line is going to use this assortment of characters to randomly select from. Adding or removing characters would allow you to customize the characters that make up the password.

You can read up on the join, choice and randint functions with a quick google search. It might make more sense if this code was broken up into more lines and variables. There's quite a bit of short hand smushed into a single line.

import string
import random
characters = string.ascii_letters + string.punctuation  + string.digits

password = ""
password_length = random.randint(8, 16)

for x in range(password_length):
    char = random.choice(characters)
    password = password + char

print password
Sign up to request clarification or add additional context in comments.

6 Comments

what is the use of giving from random import * ? can i give it just 'import random' ?
you can use import random and that is the more pythonic way to do things... that way you know what methods come from what library. If you us import random you would need to change choice to random.choice. See revised answer.
initially there would be null value stored in password variable?
@ abaldwin99..thnk u for ur input
The password variable is an empty string initially. Python has a value of None but an empty string is inherently different from that.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.