简体   繁体   English

检查字符串中的字符是否与列表的索引范围匹配?

[英]Check is a character in a string matches with a range of indexes of a list?

I am trying to create a password generator. 我正在尝试创建一个密码生成器。 I have figured out how to ensure my password has a number, uppercase and lowercase character. 我已经弄清楚了如何确保密码包含数字,大写和小写字符。 The password is being generated randomly from the string.printable function. 密码是从string.printable函数随机生成的。

Here is the part of string.printable which I am using as accepted characters in my password: 这是string.printable的一部分,我将其用作密码中的可接受字符:

0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~

I am trying to ensure that at least one of the character falls withing the range of indexes from ! 我正在尝试确保至少一个字符落入索引的范围从! to ~ which will ensure I have a special character in my password. ~ ,这将确保我的密码中包含特殊字符。 Can someone help me with this. 有人可以帮我弄这个吗。

Here is my code: 这是我的代码:

import random
import string


def password_generator(length):
    while True:
        my_password = ''
        for eachItem in range(length):
            my_password += string.printable[random.randint(0, len(string.printable) - 7)]
        if (any(x.isupper() for x in my_password) and any(x.islower() for x in my_password)
                and any(x.isdigit() for x in my_password)):
            return my_password


pass_length = int(input("Enter desired length of password: "))
print(password_generator(pass_length))

Easier: 更轻松:

  • choose 1 random letter of each special group 每个特殊组随机选择1个字母
  • chose remaining letters of all possibible characters 选择所有可能的字符的剩余字母
  • mix them up and join letters into a word 混合起来,将字母拼成一个词

import random
import string 

def password_generator(length):

    allchar = string.printable  # that is about what your choice of characters is
    low = string.ascii_lowercase
    hig = string.ascii_uppercase
    spec = string.punctuation
    nums = string.digits

    pw = []

    # ensure that each group has at least 1 character of these groups
    pw.append(random.choice(low))
    pw.append(random.choice(hig))
    pw.append(random.choice(spec))
    pw.append(random.choice(nums)) 

    # and fill up with random from all groups
    pw.extend(random.choices(allchar, k=length-len(pw)))

    # mix the positions up
    random.shuffle(pw)
    # return as string
    return "".join(pw)

pass_length = int(input("Enter desired length of password: "))
for _ in range(10):
    print(password_generator(pass_length))

Output: 输出:

b]gDEK7:wM9_T__N:ugO
l1c2p3"rW)FB@=k]'1p~
9U=M'R3"Kbzqo/~8+Dr{
g`_w7tvL#Ulto&Q4Qi]"
O7(DttWffx4N7lr~B)h$
Azd2[HHTn:X!L\5^'\~`
%Sq}be2V<\eM^$$;)V@\
1}W{iBhV;u<D2@f5\m8P
E3vmhWxaWR'9hMeiU+1$

Your main propose is to generate passwords that contain special characters, not to check if these passwords contain special characters, but anyway, if you want to check it, just use the following code: 您的主要建议是生成包含特殊字符的密码,而不是检查这些密码是否包含特殊字符,但是无论如何,如果要检查它,只需使用以下代码:

start = string.printable.find('!')
end = string.printable.find('~') + 1
special_chars = string.printable[start:end]
has_special_character = any(map(lambda c: c in chars, my_password))

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

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