0
How to check a password string contain more than 2 special character in python????
2 Answers
+ 5
Special character does normally mean a character, that is not an alphanumerical character or a space. The most common of these characters are defined in string.punctuation. The characters can be get as a variable by using:
from string import punctuation as punct # 'punct' is an alias name for punctuation
punct is now a string variable that contains:
'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~' # if additional special characters are required, they con be added to this string.
now a for loop ( or for advanced users a comprehension) can be used to iterate on the pasword string and check if loop variable is in punct, and if so to increment a counter.
+ 1
If you assume that a special character is a character which is not an alphabet or number, you could use string methods:
str.isdigit
(checks whether all characters in a string are digits)
str.isalpha
(checks whether all characters in a string are alphabets).
You would need an integer variable which counts special characters.
And you would need to iterate through all the characters in the wanted string.
In each iteration you use the string methods for the characters:
character.isdigit()
character.isalpha()
You get 2 booleans, if both are False, the counter will be incremented by 1.