creating a strong password [Python]
Need to create a function that validates my password strength having trouble with the third question.
- Both entered passwords are identical.
- The password is >= 8 characters in length.
3.If the first and last characters of the password are alphabetic then the first character of the password must be a different alphabetic letter to the last character of the password regardless of case. e.g 1st char is A so last char cannot be A or a.
def validate_password(first_pwd, second_pwd):
#Checks if password are same
if first_pwd == second_pwd:
#Checks if password is greater or equal to 8 characters
if len(first_pwd) >= 8:
#Checks if last and first character are alphabetic
if first_pwd[0].islower() and first_pwd[-1].isupper() or first_pwd[0].isupper() and first_pwd[-1].islower():
return True
else:
return False
print(validate_password("Abcd1234","Abcd1234"))
How do i ignore the digits at the end of the password and look at the nearest alphabetic letter which would be the d.
python function
add a comment |
Need to create a function that validates my password strength having trouble with the third question.
- Both entered passwords are identical.
- The password is >= 8 characters in length.
3.If the first and last characters of the password are alphabetic then the first character of the password must be a different alphabetic letter to the last character of the password regardless of case. e.g 1st char is A so last char cannot be A or a.
def validate_password(first_pwd, second_pwd):
#Checks if password are same
if first_pwd == second_pwd:
#Checks if password is greater or equal to 8 characters
if len(first_pwd) >= 8:
#Checks if last and first character are alphabetic
if first_pwd[0].islower() and first_pwd[-1].isupper() or first_pwd[0].isupper() and first_pwd[-1].islower():
return True
else:
return False
print(validate_password("Abcd1234","Abcd1234"))
How do i ignore the digits at the end of the password and look at the nearest alphabetic letter which would be the d.
python function
1
While this question has technically been answered... a far better approach would be to use an established library like zxcvbn-python if you want to actually check password strength. What you have here is extremely arbitrary and will allow such excellent passwords as'aaaaaaab','password','11111111', and'username'.
– kungphu
Nov 13 '18 at 7:57
@mikuszefski You're attributing arguments to me that I did not make. All I did was point out how comically bad this specific idea of password strength is; unless you disagree with that, I don't think you and I have any disagreement.
– kungphu
Nov 13 '18 at 8:27
there are other attributes such as There are no more than 2 vowels in the password and The password has at least 1 alphabetic character in upper case and 1 alphabetic character in lower case. I just didn't feel the need to post them because they have been solved I was just having trouble with Question 3.
– Mitchell Dehn
Nov 13 '18 at 8:59
1
@kungphu true! Basically, my comment holds for the OP then. Cheers.
– mikuszefski
Nov 13 '18 at 9:15
add a comment |
Need to create a function that validates my password strength having trouble with the third question.
- Both entered passwords are identical.
- The password is >= 8 characters in length.
3.If the first and last characters of the password are alphabetic then the first character of the password must be a different alphabetic letter to the last character of the password regardless of case. e.g 1st char is A so last char cannot be A or a.
def validate_password(first_pwd, second_pwd):
#Checks if password are same
if first_pwd == second_pwd:
#Checks if password is greater or equal to 8 characters
if len(first_pwd) >= 8:
#Checks if last and first character are alphabetic
if first_pwd[0].islower() and first_pwd[-1].isupper() or first_pwd[0].isupper() and first_pwd[-1].islower():
return True
else:
return False
print(validate_password("Abcd1234","Abcd1234"))
How do i ignore the digits at the end of the password and look at the nearest alphabetic letter which would be the d.
python function
Need to create a function that validates my password strength having trouble with the third question.
- Both entered passwords are identical.
- The password is >= 8 characters in length.
3.If the first and last characters of the password are alphabetic then the first character of the password must be a different alphabetic letter to the last character of the password regardless of case. e.g 1st char is A so last char cannot be A or a.
def validate_password(first_pwd, second_pwd):
#Checks if password are same
if first_pwd == second_pwd:
#Checks if password is greater or equal to 8 characters
if len(first_pwd) >= 8:
#Checks if last and first character are alphabetic
if first_pwd[0].islower() and first_pwd[-1].isupper() or first_pwd[0].isupper() and first_pwd[-1].islower():
return True
else:
return False
print(validate_password("Abcd1234","Abcd1234"))
How do i ignore the digits at the end of the password and look at the nearest alphabetic letter which would be the d.
python function
python function
asked Nov 13 '18 at 7:20
Mitchell DehnMitchell Dehn
133
133
1
While this question has technically been answered... a far better approach would be to use an established library like zxcvbn-python if you want to actually check password strength. What you have here is extremely arbitrary and will allow such excellent passwords as'aaaaaaab','password','11111111', and'username'.
– kungphu
Nov 13 '18 at 7:57
@mikuszefski You're attributing arguments to me that I did not make. All I did was point out how comically bad this specific idea of password strength is; unless you disagree with that, I don't think you and I have any disagreement.
– kungphu
Nov 13 '18 at 8:27
there are other attributes such as There are no more than 2 vowels in the password and The password has at least 1 alphabetic character in upper case and 1 alphabetic character in lower case. I just didn't feel the need to post them because they have been solved I was just having trouble with Question 3.
– Mitchell Dehn
Nov 13 '18 at 8:59
1
@kungphu true! Basically, my comment holds for the OP then. Cheers.
– mikuszefski
Nov 13 '18 at 9:15
add a comment |
1
While this question has technically been answered... a far better approach would be to use an established library like zxcvbn-python if you want to actually check password strength. What you have here is extremely arbitrary and will allow such excellent passwords as'aaaaaaab','password','11111111', and'username'.
– kungphu
Nov 13 '18 at 7:57
@mikuszefski You're attributing arguments to me that I did not make. All I did was point out how comically bad this specific idea of password strength is; unless you disagree with that, I don't think you and I have any disagreement.
– kungphu
Nov 13 '18 at 8:27
there are other attributes such as There are no more than 2 vowels in the password and The password has at least 1 alphabetic character in upper case and 1 alphabetic character in lower case. I just didn't feel the need to post them because they have been solved I was just having trouble with Question 3.
– Mitchell Dehn
Nov 13 '18 at 8:59
1
@kungphu true! Basically, my comment holds for the OP then. Cheers.
– mikuszefski
Nov 13 '18 at 9:15
1
1
While this question has technically been answered... a far better approach would be to use an established library like zxcvbn-python if you want to actually check password strength. What you have here is extremely arbitrary and will allow such excellent passwords as
'aaaaaaab', 'password', '11111111', and 'username'.– kungphu
Nov 13 '18 at 7:57
While this question has technically been answered... a far better approach would be to use an established library like zxcvbn-python if you want to actually check password strength. What you have here is extremely arbitrary and will allow such excellent passwords as
'aaaaaaab', 'password', '11111111', and 'username'.– kungphu
Nov 13 '18 at 7:57
@mikuszefski You're attributing arguments to me that I did not make. All I did was point out how comically bad this specific idea of password strength is; unless you disagree with that, I don't think you and I have any disagreement.
– kungphu
Nov 13 '18 at 8:27
@mikuszefski You're attributing arguments to me that I did not make. All I did was point out how comically bad this specific idea of password strength is; unless you disagree with that, I don't think you and I have any disagreement.
– kungphu
Nov 13 '18 at 8:27
there are other attributes such as There are no more than 2 vowels in the password and The password has at least 1 alphabetic character in upper case and 1 alphabetic character in lower case. I just didn't feel the need to post them because they have been solved I was just having trouble with Question 3.
– Mitchell Dehn
Nov 13 '18 at 8:59
there are other attributes such as There are no more than 2 vowels in the password and The password has at least 1 alphabetic character in upper case and 1 alphabetic character in lower case. I just didn't feel the need to post them because they have been solved I was just having trouble with Question 3.
– Mitchell Dehn
Nov 13 '18 at 8:59
1
1
@kungphu true! Basically, my comment holds for the OP then. Cheers.
– mikuszefski
Nov 13 '18 at 9:15
@kungphu true! Basically, my comment holds for the OP then. Cheers.
– mikuszefski
Nov 13 '18 at 9:15
add a comment |
2 Answers
2
active
oldest
votes
You might try to solve it with a regex. This checks if the first occurrence of a letter equals (case-insensitively) the last occurrence of a letter.
You have to adapt the regex or the if clause if you really just want to check the first char of the string or the last one.
You also can do an extra check to see if the last char in the pwd equals the last letter in the list chars below.
import re
def validate_password(first_pwd, second_pwd):
# Checks if password are same
if first_pwd == second_pwd:
# Checks if password is greater or equal to 8 characters
if len(first_pwd) >= 8:
# get only the letters of the password
chars = re.findall(r'[A-Za-z]', first_pwd)
# compare first and last occurrence of letters in pwd
if chars[0].islower() and chars[-1].isupper() or chars[0].isupper() and chars[-1].islower():
return True
else:
return False
print(validate_password("Abcd1234","Abcd1234"))
I also think, it might be enough only to check
if chars[0].upper() == chars[-1].upper(): # optional if needed: and first_pwd[-1].upper() == chars[-1].upper()
add a comment |
I'm not an expert, but as someone who tries to continually improve at python it's recommended to write out the words instead of using abbreviations.
also you rother functions to verify the password strength are being checked only if the password was the same as the previous, so make sure you take length greater than 8 out of the first check
Things worth looking at would be:
islower, isalpha HERE
with lower() HERE
and I recommend youtubing regex because it's a tough concept imo
import re
def validate_password(first_password, second_password):
if first_password == second_password:
# Cannot be the previous password
return False
letters = "".join(re.findall("[a-zA-Z]+", first_password))
first_letter = letters[0]
last_letter = letters[-1]
if len(first_password) >= 8:
if first_letter.lower() == last_letter.lower():
# First character can't be the same as last character
return False
else:
# Every test passed
return True
else:
# password too short
return False
print(validate_password("Abcd1234","Abcd1234"))
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53275777%2fcreating-a-strong-password-python%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You might try to solve it with a regex. This checks if the first occurrence of a letter equals (case-insensitively) the last occurrence of a letter.
You have to adapt the regex or the if clause if you really just want to check the first char of the string or the last one.
You also can do an extra check to see if the last char in the pwd equals the last letter in the list chars below.
import re
def validate_password(first_pwd, second_pwd):
# Checks if password are same
if first_pwd == second_pwd:
# Checks if password is greater or equal to 8 characters
if len(first_pwd) >= 8:
# get only the letters of the password
chars = re.findall(r'[A-Za-z]', first_pwd)
# compare first and last occurrence of letters in pwd
if chars[0].islower() and chars[-1].isupper() or chars[0].isupper() and chars[-1].islower():
return True
else:
return False
print(validate_password("Abcd1234","Abcd1234"))
I also think, it might be enough only to check
if chars[0].upper() == chars[-1].upper(): # optional if needed: and first_pwd[-1].upper() == chars[-1].upper()
add a comment |
You might try to solve it with a regex. This checks if the first occurrence of a letter equals (case-insensitively) the last occurrence of a letter.
You have to adapt the regex or the if clause if you really just want to check the first char of the string or the last one.
You also can do an extra check to see if the last char in the pwd equals the last letter in the list chars below.
import re
def validate_password(first_pwd, second_pwd):
# Checks if password are same
if first_pwd == second_pwd:
# Checks if password is greater or equal to 8 characters
if len(first_pwd) >= 8:
# get only the letters of the password
chars = re.findall(r'[A-Za-z]', first_pwd)
# compare first and last occurrence of letters in pwd
if chars[0].islower() and chars[-1].isupper() or chars[0].isupper() and chars[-1].islower():
return True
else:
return False
print(validate_password("Abcd1234","Abcd1234"))
I also think, it might be enough only to check
if chars[0].upper() == chars[-1].upper(): # optional if needed: and first_pwd[-1].upper() == chars[-1].upper()
add a comment |
You might try to solve it with a regex. This checks if the first occurrence of a letter equals (case-insensitively) the last occurrence of a letter.
You have to adapt the regex or the if clause if you really just want to check the first char of the string or the last one.
You also can do an extra check to see if the last char in the pwd equals the last letter in the list chars below.
import re
def validate_password(first_pwd, second_pwd):
# Checks if password are same
if first_pwd == second_pwd:
# Checks if password is greater or equal to 8 characters
if len(first_pwd) >= 8:
# get only the letters of the password
chars = re.findall(r'[A-Za-z]', first_pwd)
# compare first and last occurrence of letters in pwd
if chars[0].islower() and chars[-1].isupper() or chars[0].isupper() and chars[-1].islower():
return True
else:
return False
print(validate_password("Abcd1234","Abcd1234"))
I also think, it might be enough only to check
if chars[0].upper() == chars[-1].upper(): # optional if needed: and first_pwd[-1].upper() == chars[-1].upper()
You might try to solve it with a regex. This checks if the first occurrence of a letter equals (case-insensitively) the last occurrence of a letter.
You have to adapt the regex or the if clause if you really just want to check the first char of the string or the last one.
You also can do an extra check to see if the last char in the pwd equals the last letter in the list chars below.
import re
def validate_password(first_pwd, second_pwd):
# Checks if password are same
if first_pwd == second_pwd:
# Checks if password is greater or equal to 8 characters
if len(first_pwd) >= 8:
# get only the letters of the password
chars = re.findall(r'[A-Za-z]', first_pwd)
# compare first and last occurrence of letters in pwd
if chars[0].islower() and chars[-1].isupper() or chars[0].isupper() and chars[-1].islower():
return True
else:
return False
print(validate_password("Abcd1234","Abcd1234"))
I also think, it might be enough only to check
if chars[0].upper() == chars[-1].upper(): # optional if needed: and first_pwd[-1].upper() == chars[-1].upper()
edited Nov 13 '18 at 7:58
answered Nov 13 '18 at 7:51
zyprozypro
6352828
6352828
add a comment |
add a comment |
I'm not an expert, but as someone who tries to continually improve at python it's recommended to write out the words instead of using abbreviations.
also you rother functions to verify the password strength are being checked only if the password was the same as the previous, so make sure you take length greater than 8 out of the first check
Things worth looking at would be:
islower, isalpha HERE
with lower() HERE
and I recommend youtubing regex because it's a tough concept imo
import re
def validate_password(first_password, second_password):
if first_password == second_password:
# Cannot be the previous password
return False
letters = "".join(re.findall("[a-zA-Z]+", first_password))
first_letter = letters[0]
last_letter = letters[-1]
if len(first_password) >= 8:
if first_letter.lower() == last_letter.lower():
# First character can't be the same as last character
return False
else:
# Every test passed
return True
else:
# password too short
return False
print(validate_password("Abcd1234","Abcd1234"))
add a comment |
I'm not an expert, but as someone who tries to continually improve at python it's recommended to write out the words instead of using abbreviations.
also you rother functions to verify the password strength are being checked only if the password was the same as the previous, so make sure you take length greater than 8 out of the first check
Things worth looking at would be:
islower, isalpha HERE
with lower() HERE
and I recommend youtubing regex because it's a tough concept imo
import re
def validate_password(first_password, second_password):
if first_password == second_password:
# Cannot be the previous password
return False
letters = "".join(re.findall("[a-zA-Z]+", first_password))
first_letter = letters[0]
last_letter = letters[-1]
if len(first_password) >= 8:
if first_letter.lower() == last_letter.lower():
# First character can't be the same as last character
return False
else:
# Every test passed
return True
else:
# password too short
return False
print(validate_password("Abcd1234","Abcd1234"))
add a comment |
I'm not an expert, but as someone who tries to continually improve at python it's recommended to write out the words instead of using abbreviations.
also you rother functions to verify the password strength are being checked only if the password was the same as the previous, so make sure you take length greater than 8 out of the first check
Things worth looking at would be:
islower, isalpha HERE
with lower() HERE
and I recommend youtubing regex because it's a tough concept imo
import re
def validate_password(first_password, second_password):
if first_password == second_password:
# Cannot be the previous password
return False
letters = "".join(re.findall("[a-zA-Z]+", first_password))
first_letter = letters[0]
last_letter = letters[-1]
if len(first_password) >= 8:
if first_letter.lower() == last_letter.lower():
# First character can't be the same as last character
return False
else:
# Every test passed
return True
else:
# password too short
return False
print(validate_password("Abcd1234","Abcd1234"))
I'm not an expert, but as someone who tries to continually improve at python it's recommended to write out the words instead of using abbreviations.
also you rother functions to verify the password strength are being checked only if the password was the same as the previous, so make sure you take length greater than 8 out of the first check
Things worth looking at would be:
islower, isalpha HERE
with lower() HERE
and I recommend youtubing regex because it's a tough concept imo
import re
def validate_password(first_password, second_password):
if first_password == second_password:
# Cannot be the previous password
return False
letters = "".join(re.findall("[a-zA-Z]+", first_password))
first_letter = letters[0]
last_letter = letters[-1]
if len(first_password) >= 8:
if first_letter.lower() == last_letter.lower():
# First character can't be the same as last character
return False
else:
# Every test passed
return True
else:
# password too short
return False
print(validate_password("Abcd1234","Abcd1234"))
edited Nov 13 '18 at 8:13
answered Nov 13 '18 at 7:53
DavidhallDavidhall
259
259
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53275777%2fcreating-a-strong-password-python%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
While this question has technically been answered... a far better approach would be to use an established library like zxcvbn-python if you want to actually check password strength. What you have here is extremely arbitrary and will allow such excellent passwords as
'aaaaaaab','password','11111111', and'username'.– kungphu
Nov 13 '18 at 7:57
@mikuszefski You're attributing arguments to me that I did not make. All I did was point out how comically bad this specific idea of password strength is; unless you disagree with that, I don't think you and I have any disagreement.
– kungphu
Nov 13 '18 at 8:27
there are other attributes such as There are no more than 2 vowels in the password and The password has at least 1 alphabetic character in upper case and 1 alphabetic character in lower case. I just didn't feel the need to post them because they have been solved I was just having trouble with Question 3.
– Mitchell Dehn
Nov 13 '18 at 8:59
1
@kungphu true! Basically, my comment holds for the OP then. Cheers.
– mikuszefski
Nov 13 '18 at 9:15