Python - 输出列表

时间:2015-10-12 18:34:23

标签: python

我正在python中创建一个程序,它从用户的输入生成一个随机密码。到目前为止我的代码是:

from random import *

symbols = ['!','"','£','$','%','^','&','*','(',')','-','_','=','/','*','-','+','{','}','[',']',':',';','@','>','#','~','<',',','.','?']
rand1 = randint(0,32)
rand2 = randint(0,32)

fore = raw_input('Enter your forename')
sur = raw_input('Enter your Surname')
birth = raw_input('Enter you birth year')
password = symbols[rand1], fore[0:2], sur[0:3], birth[2:4], symbols[rand2]
print password

当程序输出密码时,它看起来像这样:

('+', u'jo', u'smi', u'90', '/')

有没有摆脱括号和逗号等额外字符?

2 个答案:

答案 0 :(得分:4)

尝试print ''.join(password)

在这里查看documentation。这会将您创建的元组加入到字符串中。

或者您可以使用+运算符将组件直接连接到字符串中,如@pzp所示。

查看this code example,其中显示了两种可能的方法。

答案 1 :(得分:1)

password = symbols[rand1], fore[0:2], sur[0:3], birth[2:4], symbols[rand2]

应该是

password = symbols[rand1] + fore[0:2] + sur[0:3] + birth[2:4] + symbols[rand2]