学习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 这个遍历里多了个括号

学习python 检测字符串的方法的更多相关文章
- 学习python的字符串的方法
今天看了下学习视频,发现str也有许多方法,才发现昨天了解的关于字符串知识甚少,所以今天查看了python的源码,学习一些新的方法 以下是我把边对照着源码边翻译,边尝试实现方法 1.第一个字符有大写字 ...
- 零基础学习 Python 之字符串
初识字符串 维基百科对于字符串的定义式:字符串是由零个或者多个字符组成的有限串行.你之前学会敲的第一行 print 代码里的 "Hello World",就是一个字符串.字符串的本 ...
- python反转字符串(简单方法)及简单的文件操作示例
Python反转字符串的最简单方法是用切片: >>> a=' >>> print a[::-1] 654321 切片介绍:切片操作符中的第一个数(冒号之前)表示切片 ...
- 【Python学习笔记】字符串拼接方法(5种)总结
字符串的 5 种拼接方法: “+”号 “,”号 直接连接 格式化 多行字符串拼接 第一种:“+”号 print("Hello"+"Python") 打印结果: ...
- Python中字符串的方法及注释
先整理到这里:用的时候便于查询.相当于自己的资料库吧. capitalize() 把字符串的第一个字符改为大写 casefold() 把整个字符串的所有字符改为小写 center(width) ...
- 身为一个小白,看到一篇值得看的文章。讲述小白学习python的6个方法。
01. Python怎么学? Python虽然号称非常简单,功能强大!但是再简单,它也是一门编程语言,任何一个编程语言都会包含: 内功,心法和招式,内功心法就是指的算法,数据结构: 招式就是任何一 ...
- python 替换字符串的方法replace()、正则re.sub()
一.replace()函数1用字符串本身的replace方法: a = 'hello word' b = a.replace('word','python') print b 1 2 3 二.re ...
- ES6学习笔记之字符串新增方法
1.字符串的子串识别 传统上,Javascript 只有indexof 方法,用来确定一个字符串是否包含在另一个字符串中.如: //indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的 ...
- python之字符串及其方法---整理集
字符串方法 1.capitalize方法:字符串首字母大写 举例: test="alex" v=test.capitalize() print(v) 返回结果: Alex 2.ca ...
随机推荐
- VCenter6.0.0的安装过程
背景和实验环境介绍 操作系统环境:windows 2008R2 中文企业版 前期环境配置 配置IP信息,把DNS改成自己的IP 修改主机名和后缀 安装和配置DNS服务 Vcenter 添加dns角色 ...
- mysql入门学习笔记
MySQL的登陆和退出 mysql -u 用户名 -p 密码 #登陆 quit #退出(exit or \q) 具体参数: 参数 描述 -D,--database=name 打开指定数据库 -deli ...
- rust visual studio editoe & debugger
step Visual studio Try VisualRust-0.1.2 (1).msi, error. find no vs2017 extension. try RustLanguageEx ...
- SRE_ Google运维解密
# 第IV部分 管理 #系统可用性时间表 # 专用术语 SLO:服务等级目标 LCE(Land-Covered Earth):紧急检修登陆艇 # 紧急事故管理 一次流程管理良好的事故 # 东西早晚要坏 ...
- 一、python中的一切皆对象
1.函数和类是对象,是Python中的一级公民 (1)将函数和类赋值给一个变量 将函数或类赋值给一个变量,就可以把该变量当成函数或类来使用 def ask(name): print(name) cla ...
- pyhanlp:hanlp的python接口
HanLP的Python接口,支持自动下载与升级HanLP,兼容py2.py3. 安装 pip install pyhanlp 使用命令hanlp来验证安装,如因网络等原因自动安装失败,可参考手动配置 ...
- System.DllNotFoundException: Unable to load DLL 'libgdiplus': The specified module could not be found.
netcore 使用System.Drawing 出现如下错误: Unhandled Exception: System.TypeInitializationException: The type i ...
- 工控随笔_20_西门子_WinCC的VBS脚本_09_常量和流程控制_02
vbs不但提供了分支结构,还提供了丰富的循环形式.一共有3种循环: 1.for循环 2.do...loop循环 3.while循环 各种循环有各自的特点,在使用的时候可以进行转换. 前面已经描述过Fo ...
- nginx1.14.0版本location路径配置四种方式
假设下面四种情况分别用 http://192.168.1.1/proxy/test.html 进行访问. 第一种:location /proxy/ { proxy_pass http:// 12 ...
- Complete Binary Search Tree
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...