Want to start my password with capital letter always?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Want to start my password with capital letter always?



I am using Python 3.6 and working on Jupyter Notebook. My code for password validation using regular expression is working absolutely great. I just want to make an enhancement that my password should start with capital letter always. I don't know how to write the regular expression in my elif loop. The code is as follows:


import re
import getpass

pattern = re.compile(r"")

while True:
my_str = getpass.getpass("Enter a password: ")

if(8>len(my_str)):
print("Password must be of 8 digits")

if re.search(r"[a-z1,9]", my_str) is None:
print("Your Password must contain 1 lowercase letter")

if re.search(r"[!@$&1,5]", my_str) is None:
print("Your Password must contain 1 special character")

if re.search(r"[A-Z1,5]", my_str) is None:
print("Your Password must contain 1 uppercase letter")

if re.search(r"d1,5", my_str) is None:
print("Your Password must contain 1 digit")
elif re.match(r"[A-Za-z0-9@#$%^&+=]8,",my_str):
pattern = re.compile(r"[A-Za-z0-9@#$%^&+=]8,")
password = pattern.match(my_str)
print(password)
break
else:
print("Not a valid Password")





Your conditions do not seem to make sense, what are the exact requirement for the password?
– Olivier Melançon
Aug 13 at 5:58





I want to start my password with capital letter always
– Abhishek Singh
Aug 13 at 5:59





rest is working fine
– Abhishek Singh
Aug 13 at 6:00





I am sorry, but the rest is not working
– Olivier Melançon
Aug 13 at 6:16





Thank You Sir...I was giving condition that, password must contain atleast 1 digit, atleast 1 Alphabet both small and capital and atleast 1 special character..with not more than 8 characters..With these conditions, it's working fine..
– Abhishek Singh
Aug 13 at 7:51





1 Answer
1



You can use this condition to test if the first character is an uppercase letter:


my_str[0].isupper()



Although, there is more that is wrong with your code



Improvements



The conditions you describe in your print statements do not seem to match what you are testing.


print



By example the condition re.search(r"[a-z1,9]", my_str) will check if the password contains a lowercase letter or any of the characters , 1, , , 9, . You probably wanted to have the brackets outside the character set.


re.search(r"[a-z1,9]", my_str)



1


,


9




Furthermore your code flow is incorrect, by example your else clause of your while loop will never be reached.


else


while



A suggestion would be to use a valid flag that turns False whenever a condition is not met, the loop is finally exited only if the flag is still True. Otherwise the unmet conditions are printed and the loop starts again.


valid


False


True



Fixed code


import re
import getpass

while True:
my_str = getpass.getpass("Enter a password: ")

valid = True

if 8 > len(my_str):
valid = False
print("Password must have at least 8 characters")

if not re.search(r"[a-z]", my_str):
valid = False
print("Your Password must contain 1 lowercase letter")

if not re.search(r"[!@$&]", my_str):
valid = False
print("Your Password must contain 1 special character")

if not re.search(r"[A-Z]", my_str):
valid = False
print("Your Password must contain 1 uppercase letter")

if not re.search(r"d", my_str):
valid = False
print("Your Password must contain 1 digit")

# This is the condition that checks the first character is a capital letter
if my_str and not my_str[0].isupper():
valid = False
print('First character must be a capital letter')

if valid:
break





Thank You Sir...will do the correction part..
– Abhishek Singh
Aug 13 at 7:54






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

Firebase Auth - with Email and Password - Check user already registered