[英]How do I remove space in between list characters when I print?
我尝试制作一个小型密码生成器,并希望最终将 output 发送到 txt 文件,但是当我打印我的列表时,每个字符之间都有空格,我找不到删除它的解决方案。
这是我的代码:
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)
这是我得到的 output 的示例。
D ~ 8 { > 5 x 0 H B 6 * ' 0 \ h 2 9 ~ { 1 p
但是我想像这样删除空间。
D~8{>5x0HB6*'0\h29~{1p
谢谢您的帮助。 我正在使用 pychrarm 和 python 3.7
使用sep
(分隔符)参数并将其设置为空字符串。
print(*mainlist, sep='')
print()
的可选 arguments 在其文档中进行了解释:
>>> 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.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.