trying to generate random passwords with "-" in between

Question:

import random
s = "abcdefghijklmnopqrstuvwxyz01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()?"
passlen = 4
p = "".join(random.sample(s,passlen))
p2 = "".join(random.sample(s,passlen))
print(p,"-",p2)

This is my code. But when i run it get something like this: RJ9e – zN0P
I do not need the spaces in between. What am i missing here?
Thanks!

Asked By: stickywicket

||

Answers:

The problem with your code is that print(p,"-",p2) will print spaces between its arguments, better use print("{}-{}".format(p, p2)).

Answered By: mpcabd

This is because of print. You need to concat password into single variable like that:
password = '%s-%s' % (p, p2)

Answered By: Kamil SokoĊ‚owski

just concat all string into single string using ‘+’ operator

import random
s = "abcdefghijklmnopqrstuvwxyz01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()?"
passlen = 4
p = "".join(random.sample(s,passlen))
p2 = "".join(random.sample(s,passlen))
print(p+"-"+p2)
Answered By: Saket Mittal
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.