在python中包含一组特定的标点符号

时间:2018-07-10 19:51:52

标签: python-3.x

我正在使用python代码生成随机密码,例如

def password_random_generator():
    min_char = 8
    max_char = 12
    all_char = string.ascii_lowercase + string.ascii_uppercase + 
               string.punctuation + string.digits
    password = "".join(random.choice(all_char) for x in 
               range(random.randint(min_char, max_char)))
    return password

我可以仅包括特定的标点集而不是python3中所有标点的方式是什么?

1 个答案:

答案 0 :(得分:0)

我在您的函数中添加了参数,以仅指定所需的标点符号:

import string, random

def password_random_generator(my_punctuation=string.punctuation):
    min_char = 8
    max_char = 12
    all_char = string.ascii_lowercase + string.ascii_uppercase + \
               my_punctuation + string.digits
    password = "".join(random.choice(all_char) for x in
               range(random.randint(min_char, max_char)))
    return password

print(password_random_generator('<=>'))

输出(例如):

  

04es = KDC