Reputation: 3
Ive tried making a small password generator and want to eventually send the output to a txt file, however when I print my list there are spaces in between each character and I cant find a solution to remove it.
Here is my code:
import string
import random
pwdlen = 0
holder = 0
mainlist = []
print('Enter Number of Characters Wanted:')
pwdlen = int(input())
print('Your', pwdlen, 'Character Password is:')
while holder != pwdlen:
dec = (random.randrange(1, 5))
holder += 1
if dec == 1:
mainlist.append(random.choice(string.digits))
elif dec == 2:
mainlist.append(random.choice(string.punctuation))
elif dec == 3:
mainlist.append(random.choice(string.ascii_uppercase))
else:
mainlist.append(random.choice(string.ascii_lowercase))
else:
print(*mainlist)
here is an example of the output I get.
D ~ 8 { > 5 x 0 H B 6 * ' 0 \ h 2 9 ~ { 1 p
But Id like to space removed like so.
D~8{>5x0HB6*'0\h29~{1p
Thanks for the help. Im using pychrarm and python 3.7
Upvotes: 0
Views: 97
Reputation: 11651
Use the sep
(separator) argument and set it to the empty string.
print(*mainlist, sep='')
print()
's optional arguments are explained in its doc:
>>> help(print)
Help on built-in function print in module builtins:
print(...)
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.
Upvotes: 1