我不明白为什么这里面有空格

2024-03-28 14:27:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经包括了整个问题陈述和使用的代码。当我尝试两种可选的char_列表结构时,这两种结构都被包含并注释掉了;问题在这两种情况下都存在。你知道吗

为什么' '.join()不清理空间?你知道吗

'''Write a password generator in Python. Be creative with how you generate passwords - strong passwords have a mix of lowercase letters, uppercase letters, numbers, and symbols. The passwords should be random, generating a new password every time the user asks for a new password. Include your run-time code in a main method.'''


import random

def password_generator():

  #char_str = '1234567890abcdefgh!@#$%(^)%($('
  #char_str = ['a', 'b', 'c', 'd', '1', '2', '3', '4']
  password = []
  length = int(input("How long should the password be?"))

  while len(password) < length:
    password.append(char_str[random.randint(0, len(char_str) - 1)])

  return(' '.join(password))

print(password_generator())

输出示例:% ) 0 4 d c f b % 7


Tags: theinnewtimerandompasswordbegenerator
2条回答

每个字符之间都有空格,这就是为什么每个字符之间都有空格的原因。要解决此问题,可以在空字符串上联接:

"".join(password)

或者,您也可以只构建一个字符串而不是一个列表:

password = ""
while len(password) < length:
    password += char_str[random.randint(0, len(char_str) - 1]

''。加入(密码)就可以了。改为加入(密码)。你知道吗

相关问题 更多 >