簡體   English   中英

打印時如何刪除列表字符之間的空格?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM