Python:统计大小写字母个数和数字个数
2023-09-14 09:16:19 时间
【问题描述】
统计一行字符的大写字母,小写字母和数字的个数。先输出大写字母个数,再输出小写字母个数,最后输出数字个数。
【输入形式】
ljaij1A
【输出形式】
1
5
1
【提示】
用字符串的方法isupper, islower来判别大小写。isdigit来判断是否是数字。
a = input()
daxie, xiaoxie, shuzi = 0, 0, 0
for i in a:
if i.islower():
xiaoxie += 1
elif i.isupper():
daxie += 1
elif i.isdigit():
shuzi += 1
print(daxie)
print(xiaoxie)
print(shuzi)
islower函数是检测字符串中字母是否都是小写字母,若全部都是由小写字母组成,则返回True,否则返回False。islower函数没有参数。
举例:
a = "this is a"
b = "this is b.1321321/*"
c = "this is c.1321321A"
print(a.islower())
print(b.islower())
print(c.islower())
输出结果:
True
True
False
isupper函数是检测字符串中字母是否都是大写字母,若全部都是由大写字母组成,则返回True,否则返回False。isupper函数没有参数。
举例:
a = 'HELLO WORLD'
b = 'Hello World'
c = 'HELLO WORLD 2019'
d = ''
e = '2019'
f = 'ÂƁȻ2019'
print(a.isupper())
print(b.isupper())
print(c.isupper())
print(d.isupper())
print(e.isupper())
print(f.isupper())
输出结果:
True #a 因为所有大小写的字符均为大写
False #b 因为有一些小写的大小写字符
True #c 因为所有大小写的字符均为大写。 字符串中的数字不是大小写字符
False #d 因为字符串为空
False #e 因为e字符串中没有任何小写的字符
True #f 因为字符串中所有大小写的字符均为大写
isdigit函数检测字符串是否只由数字组成,若全部由数字组成,则返回True,否则返回False。
举例:
a='123'
b='aa234'
c='中文'
d='12+6'
print(a.isdigit())
print(b.isdigit())
print(c.isdigit())
print(d.isdigit())
输出结果:
True
False
False
False
扩展:
isalnum() | 所有字符都是数字或者字母 |
isalpha() | 所有字符都是字母 |
isdigit() | 所有字符都是数字 |
islower() | 所有字符都是小写 |
isupper() | 所有字符都是大写 |
istitle() | 所有单词都是首字母大写,像标题 |
isspace() | 所有字符都是空白字符、\t、\n、\r |
相关文章
- Python入门系列(十)一篇学会python文件处理
- python中sqrt函数用法_Python : sqrt() 函数
- python爬虫—–Python访问http的几种方式「建议收藏」
- java与python-如何对比Python和Java,只需三分钟告诉你!
- 【说站】python格式字符串是什么
- 经纬度距离计算 python_Python已知两坐标求距离
- centos 安装 python3_python加密解密库
- python pandas VS excel给成绩赋值等级
- python attrs_Python attrs作用是什么?
- Python字典提取_python字典键对应的值
- python hexdump_笨办法学 Python · 续 练习 26:`hexdump`
- python做微信回复机器人_Python自动化脚本
- Python数据类型(列表)
- 上手python之while循环和for循环
- Python 自动化指南(繁琐工作自动化)第二版:一、PYTHON 基础知识
- Python – 爬取酷狗音乐
- python-Python与MongoDB数据库-MongoDB数据库的基本知识
- Python编程学习,高效求解素数程序实例详解编程语言
- Python变量作用域(通俗易懂)
- 快速查询MySQL数据库:Python篇(python查询mysql数据库)
- Linux 启动 Python编程之旅(linux打开python)
- Python操作Redis实现数据持久化(python操作redis)
- 在Linux上学习Python——你的编程之路(linux学python)
- Python如何使用MySQL构建立连接(python怎么连接mysql)
- Linux下从系统卸载Python(linux下卸载python)
- Linux下Python编程:从入门到精通(linux下python编程)
- Linux下的Python开发技术之旅(linux下python开发)
- SublimeText2编译python出错的解决方法(Thesystemcannotfindthefilespecified)
- Python统计列表中的重复项出现的次数的方法
- python进阶教程之动态类型详解