IT & IoT Security | Cloud | It's all about the life itself

Nothing in life is as important as you think it is, while you are thinking about it.

Python – Valid character list

Hi, let us check the given string against a valid character list. There might be numerous way to do this. Here is a sample one.

 

str_to_check = "1234-3*33?3+----!"
valid_chars = "0123456789-+"
uyan_char_sayisi = 0
uymayan_listesi = ""
uymayan_char_sayisi = 0

str_to_check_uzunluk = len(str_to_check)
valid_chars_uzunluk = len(valid_chars)
#print(str_to_check_uzunluk)
for i in range(0,str_to_check_uzunluk):
    for j in range(0,valid_chars_uzunluk):
        if str_to_check[i] == valid_chars[j]:
            uyan_char_sayisi += 1
    if not str_to_check[i] in valid_chars:
        uymayan_listesi += str_to_check[i]

if uyan_char_sayisi == str_to_check_uzunluk:
    print("String is valid")
else:
    print("String is invalid")
print("Here is/are the invalid character(s) :",uymayan_listesi)

The output will be;
String is invalid
Here is/are the invalid character(s) : *?!