Pythonで文字列内の小文字の数を数える方法は何ですか?
Pythonの組み込み関数islower()とcount()を使用して、文字列内の小文字の数を数えることができます。以下にサンプルコードを示します:
def count_lowercase(string):
count = 0
for char in string:
if char.islower():
count += 1
return count
# 示例用法
string = "Hello World"
lowercase_count = count_lowercase(string)
print(lowercase_count)
このコードを実行すると、出力結果は次の通りです:
8
このことは、文字列に8つの小文字のアルファベットが含まれていることを示しています。