简体   繁体   English

如何将 Python 列表打印为字符串

[英]How to Print Python Lists as String

I would like to know how to print python lists as String instead of list.我想知道如何将 python 列表打印为字符串而不是列表。 When I add the three lists I get the following result当我添加三个列表时,我得到以下结果

['a', 'b', 'c', 'd', '0', '1', ',', '#']

I want this output:我想要这个输出:

abcd01!#

import random
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
symbols = ['!', '#', '$', '%', '&', '(', ')', '*', '+']

print("Welcome to the PyPassword Generator!")
nr_letters= int(input("How many letters would you like in your password?\n")) 
nr_symbols = int(input(f"How many symbols would you like?\n"))
nr_numbers = int(input(f"How many numbers would you like?\n"))

#Eazy Level - Order not randomised:
#e.g. 4 letter, 2 symbol, 2 number = JduE&!91


letter_pick = letters[0:nr_letters]
number_pick = numbers[0:nr_symbols]
symbol_pick = symbols[0:nr_numbers]

print (letter_pick+number_pick+symbol_pick)`

expecting期待

abcd01!#

instead of代替

['a', 'b', 'c', 'd', '0', '1', '!', '#']

You can use join to join all element of list.您可以使用join来连接列表的所有元素。

print(''.join(letter_pick+number_pick+symbol_pick))

if you only want to print the data.如果您只想打印数据。 Use this.用这个。

print(*(letter_pick+number_pick+symbol_pick), sep='')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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