0

I've written a simple python password generator:

import string
import random

print "Password generator will create a random customizable password."
print "Choose your options wisely."

number = int(raw_input("How many letters do you want in your password?"))
caps = str(raw_input("Do you want capital letters in your password? Y/N:"))
symbols = str(raw_input( "Do you want punctuation and other symbols in your password?    Y/N:"))
otherchoice = str(raw_input( "Do you want numbers in your password? Y/N:"))

punctuation = ("!", ".", ":", ";", ",", "?", "'", "@", "$", "~", "^","%", "#", "&", "/")
numbers = map(str,range(0,10))
stringpunctuation = "".join(punctuation)
stringnumbers = "".join(numbers)
lowercase = string.ascii_lowercase
uppercase = string.ascii_uppercase

if caps == "Y":
    characters = lowercase + uppercase
else:
    characters = lowercase

if symbols == "Y":
    characters += stringpunctuation
if otherchoice == "Y":
    characters += stringnumbers

password = random.sample(characters, number)
print "The password is", password

This is an example of what appears in the terminal when I run it:

Password generator will create a random customizable password.
Choose your options wisely.
How many letters do you want in your password?15
Do you want capital letters in your password? Y/N:Y
Do you want punctuation and other symbols in your password? Y/N:Y
Do you want numbers in your password? Y/N:Y
The password is ['x', 'p', 'E', 'X', 'V', '#', ',', '@', 'q', 'N', 'F', 'U', 'b', 'W', '.']

How can I make it so that the output is something like this (using the password in the example): xpEXV#,@qNFUbW.

I don't really need to know the answer, the practical result will be the same, I'm just super curious.

1 Answer 1

4

Join the characters to together with the str.join() method; pick a joining string and call the method on that, passing in your list:

password = ''.join(password)

This joins the characters with the empty string (''):

>>> password = ['x', 'p', 'E', 'X', 'V', '#', ',', '@', 'q', 'N', 'F', 'U', 'b', 'W', '.']
>>> ''.join(password)
'xpEXV#,@qNFUbW.'

For other uses you could pick a different joiner:

>>> '->'.join(password)
'x->p->E->X->V->#->,->@->q->N->F->U->b->W->.'
>>> '...'.join(password)
'x...p...E...X...V...#...,[email protected]....'
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot, I feel so stupid now, specially since I already knew how to use "".join. I'll accept your answer as soon as possible

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.