学习python 检测字符串的方法

检测字符串长度的方法:len()

检测字符串是否含有字母的方法:str.isalpha()

检测字符串是否含有数字的方法:str.isnumeric()

检测字符串是否有大写字母:str.upper()

检测字符串是否含有小写字母:str.lower  

更多is.xxx的方法,请参考:

https://docs.python.org/3/library/sdtypes.html#string - methods

代码案例:

def check_numbers_exist(password_str):
"""
判断字符串是否含有数字
"""
for c in password_str:
if c.isnumeric():
return True
return False
def check_letter_exit(password_str):
"""
判断字符串是否含有字母
"""
for c in password_str:
if c.isalpha():
return True
return False

def main():
"""
主函数
:return:
"""
password = input("请输入您的密码:")
#密码强度
strength_password = 0
#规则1
if len(password) >= 8:
strength_password += 1
else:
print("密码长度至少是8位!")
#规则2
if check_numbers_exist(password):
strength_password += 1
else:
print("密码没有含有数字")
#规则3
if check_letter_exit(password):
strength_password += 1
else :
print("密码没有含有字母")
if strength_password == 3:
print("密码强度合格!")
else :
print("密码强度不合格!")


if __name__ == '__main__':
main()
 学习编写这个代码出现了一个小错误,控制台报错是:str' object is not callable

Traceback (most recent call last):
File "D:/Program Files (x86)/pycharm install/pycharm wenjianjia/Password/Password_V1.0.py", line 61, in <module>
main()
File "D:/Program Files (x86)/pycharm install/pycharm wenjianjia/Password/Password_V1.0.py", line 45, in main
if check_numbers_exist(password):
File "D:/Program Files (x86)/pycharm install/pycharm wenjianjia/Password/Password_V1.0.py", line 21, in check_numbers_exist
for c in password_str():
TypeError: 'str' object is not callable

原来是我之前在编写的时候粗心在for c in password 这个遍历里多了个括号

猜你喜欢

转载自www.cnblogs.com/minxinstudy/p/10564090.html