繁体   English   中英

如何在python中正确设置字符串格式以生成密码?

[英]How do i properly format strings in python for a password generator?

所以我想做的是打印出带有数字和特殊字符的单词列表。 现在,我目前停留在打印单词上,而单词print的范围是生成的数字。

我已经尝试过:

word = input("Enter a word\n>")
firstLetter = word[0]
firstLetter = firstLetter.upper()
length = len(word)
newWord = firstLetter + word[1:length]
print("%s \n".join([str(num).zfill(2) for num in range(0, 10)]) % newWord)

我试过了:

word = input("Enter a word\n>")
firstLetter = word[0]
firstLetter = firstLetter.upper()
length = len(word)
newWord = firstLetter + word[1:length]
print("\n".join([str(num).zfill(2) for num in range(0, 10)]) % newWord)

我期望的是这样的:

Password01
Password02
Password03
Password04
Password05
Password06
Password07
Password08

等等

我的结果:

Python 3.6.1 (default, Dec 2015, 13:05:11)
[GCC 4.8.2] on linux
Enter a word
>pop
Traceback (most recent call last):
  File "main.py", line 8, in <module>
    print("\n".join([str(num).zfill(2) for num in range(0, 10)]) % newWord)
TypeError: not all arguments converted during string formatting

您可以将.title()大写为字符串的第一个字母,并使用range()生成数字并使用f字符串设置其格式:

word = input("Enter a word\n>")
word = word.title()
for x in range(1, 9):
    print(f'{word}0{x}')

注意 :如果使用列表理解的全部目的不是生成列表,则不要使用列表理解。

您可以使用大写字母来更改单词首字母的大小写。 另外,用于附加输入密码的密码是错误的。

试试下面的代码:

word = raw_input("Enter a word\n>")
word = word.capitalize()
for i in range(1, 10):
    print (word + '0' + str(i))

输出:

Enter a word
>password
Password01
Password02
Password03
Password04
Password05
Password06
Password07
Password08
Password09

暂无
暂无

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

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