在 Python 程序中统计字符串中出现的英文小写字符数量
如果需要统计字符串中出现的小写字符数量,可以使用 ‘islower’ 方法和一个简单的 ‘for’ 循环。
以下是演示示例 −
示例
my_string = "Hi there how are you"
print("The string is ")
print(my_string)
my_counter=0
for i in my_string:
if(i.islower()):
my_counter=my_counter+1
print("The number of lowercase characters in the string are :")
print(my_counter)输出
The string is Hi there how are you The number of lowercase characters in the string are : 15
解释
一个字符串被定义并显示在控制台上。
一个计数器值被初始化为 0。
该字符串被迭代,并使用 ‘islower’ 方法检查它是否包含小写字母。
如果是,则计数器增 1,直到字符串结束。
这将作为输出显示在控制台上。
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP