Python程序中计算字符串中小写字符的个数
当需要计算字符串中小写字符的数量时,可以使用'islower'方法和简单的'for'循环。
以下是相同的演示-
示例
my_string = "Hi there how are you"
print("字符串是 ")
print(my_string)
my_counter=0
for i in my_string:
if(i.islower()):
my_counter=my_counter+1
print("字符串中的小写字符数为:")
print(my_counter)输出结果字符串是 Hi there how are you 字符串中的小写字符数为: 15
解释
一个字符串被定义并显示在控制台上。
计数器值初始化为0。
字符串被迭代,并使用'islower'方法检查它是否包含小写字母。
如果是,则计数器加1,直到字符串结束。
这在控制台上显示为输出。