Computer >> 컴퓨터 >  >> 프로그램 작성 >> Python

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씩 증가합니다.

  • 이것은 콘솔에 출력으로 표시됩니다.