python - 我如何知道用户输入是否包含python中的所有字符或数字
问题描述
我正在使用 PyCharm,并希望知道如何设置我的字符串/变量来告诉我用户输入是全字母字符还是全数字?这是一个伪代码示例:
weak = all characters or numbers
strong = a mix of characters and numbers
INPUT password
'isaplpha' 或 'isnumeric' 可以在这种情况下派上用场吗?如果是这样,怎么做?
解决方案
是的!你是对的,isalpha
正是isnumeric
你想要的。
第一次测试
what = 'letters'
if what.isalpha():
print('only letters')
elif what.isnumeric():
print('only numbers')
输出
'only letters'
第二次测试
what = "123"
if what.isalpha():
print('only letters')
elif what.isnumeric():
print('only numbers')
输出
only numbers
推荐阅读
- django - Sum 的注释似乎不起作用 Django
- hyperledger-fabric - 获取错误为 ccenv:latest 从 docker hub 删除导致链码实例化问题
- c# - 引用 COM+ 元素时对象与目标类型不匹配
- reactjs - 选择中的antd新图标不可点击
- html - 设备更改时如何使容器响应?
- c# - 我如何使用 ajax 和 asp.net asmx 页面为我的 html 表设置分页
- c - 当用户输入错误的因素时,我想提供错误信息
- python - Gunicorn Gevent 运行 Python3.7
- javascript - 当注册是仅电子邮件登录时如何自动测试用户注册
- java - Spring Security 和 Azure:是否有 Active Directory 组通配符?