简体   繁体   English

打印时如何删除列表字符之间的空格?

[英]How do I remove space in between list characters when I print?

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.我尝试制作一个小型密码生成器,并希望最终将 output 发送到 txt 文件,但是当我打印我的列表时,每个字符之间都有空格,我找不到删除它的解决方案。

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.这是我得到的 output 的示例。

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我正在使用 pychrarm 和 python 3.7

Use the sep (separator) argument and set it to the empty string.使用sep (分隔符)参数并将其设置为空字符串。

print(*mainlist, sep='')

print() 's optional arguments are explained in its doc: 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM