Python Program to Count Number of Lowercase Characters in a String

Strings in Python:

In Python, a string may be a sequence of characters. It is an information type that has been derived. Strings are unchangeable. This means that when they have been defined, they can not be modified. Many Python functions change strings, like replace(), join(), and split(). They do not, however, alter the original string. They make a duplicate of a string, alter it, then return it to the caller.

Given a string, the task is to write a python program to calculate the total number of lowercase characters in the given string.

Examples:

Example1:

Input:

given string = 'Hello this is BTechGeeks'

Output:

The total number of lower case letters present in the given string [ Hello this is BTechGeeks ] =  17

Example2:

Input:

given string = BTechgeeks

Output:

The total number of lower case letters present in the given string [ BTechgeeks ] = 8

Program to Count Number of Lowercase Characters in a String in Python

There are several ways to count the number of lowercase characters in the given string some of them are:

Beginners and experienced programmers can rely on these Best Java Programs Examples and code various basic and complex logics in the Java programming language with ease.

1)Using Count Variable and islower() function(Static Input)

Approach:

  • Give the string as static input and save it in a variable.
  • Take a variable to say countlow that stores the total number of lowercase characters present in the given string.
  • Initialize the countlow to 0.
  • Traverse the given string using for loop.
  • Check if the iterator value is lowercase or not using islower() function.
  • If the character is in lowercase then increment the countlow by 1.
  • Print the countlow.

Below is the implementation:

# Give the string as static input and save it in a variable.
given_strng = 'Hello this is BTechGeeks'
# Take a variable to say countlow that stores the total number of lowercase characters present in the given string.
# Initialize the countlow to 0.
countlow = 0
# Traverse the given string using for loop.
for charact in given_strng:
    # Check if the iterator value is lowercase or not using islower() function.
    if(charact.islower()):
        # If the character is in lowercase then increment the countlow by 1.
        countlow = countlow+1
# Print the countlow.
print(
    'The total number of lower case letters present in the given string [', given_strng, '] = ', countlow)

Output:

The total number of lower case letters present in the given string [ Hello this is BTechGeeks ] =  17

2)Using Count Variable and islower() function(User Input)

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Take a variable to say countlow that stores the total number of lowercase characters present in the given string.
  • Initialize the countlow to 0.
  • Traverse the given string using for loop.
  • Check if the iterator value is lowercase or not using islower() function.
  • If the character is in lowercase then increment the countlow by 1.
  • Print the countlow.

Below is the implementation:

# Give the string as user input using the input() function and store it in a variable.
given_strng = input('Enter some random string = ')
# Take a variable to say countlow that stores the total number of lowercase characters present in the given string.
# Initialize the countlow to 0.
countlow = 0
# Traverse the given string using for loop.
for charact in given_strng:
    # Check if the iterator value is lowercase or not using islower() function.
    if(charact.islower()):
        # If the character is in lowercase then increment the countlow by 1.
        countlow = countlow+1
# Print the countlow.
print('The total number of lower case letters present in the given string [', given_strng, '] = ', countlow)

Output:

Enter some random string = BTechgeeks
The total number of lower case letters present in the given string [ BTechgeeks ] = 8

Explanation:

  • A string must be entered by the user and saved in a variable.
  • The count variable is set to zero.
  • The for loop is used to go over the characters in a string.
  • When a lowercase character is encountered, the count is increased.
  • The string’s total number of lowercase characters is printed using the print() function.

3)By Checking Ascii values(Static Input)

Approach:

  • Give the string as static input and save it in a variable.
  • Take a variable to say countlow that stores the total number of lowercase characters present in the given string.
  • Initialize the countlow to 0.
  • Traverse the given string using for loop.
  • Calculate the ASCII value of the character using the ord() function and store it in a variable.
  • If the ASCII values lie between 97 to 122 then the character is in lowercase.
  • If the character is in lowercase then increment the countlow by 1.
  • Print the countlow.

Below is the implementation:

# Give the string as static input and save it in a variable.
given_strng = 'Hello this is BTechGeeks'
# Take a variable to say countlow that stores the total number of lowercase characters present in the given string.
# Initialize the countlow to 0.
countlow = 0
# Traverse the given string using for loop.
for charact in given_strng:
    # Calculate the ASCII value of the character using the ord() function and
    # store it in a variable.
    ascValue = ord(charact)
    # If the ASCII values lie between 97 to 122 then the character is in lowercase.
    if(ascValue >= 97 and ascValue <= 122):
        # If the character is in lowercase then increment the countlow by 1.
        countlow = countlow+1
# Print the countlow.
print(
    'The total number of lower case letters present in the given string [', given_strng, '] = ', countlow)

Output:

The total number of lower case letters present in the given string [ Hello this is BTechGeeks ] =  17

4)By Checking Ascii values(User Input)

Approach:

  • Give the string as user input using input() function and save it in a variable.
  • Take a variable to say countlow that stores the total number of lowercase characters present in the given string.
  • Initialize the countlow to 0.
  • Traverse the given string using for loop.
  • Calculate the ASCII value of the character using the ord() function and store it in a variable.
  • If the ASCII values lie between 97 to 122 then the character is in lowercase.
  • If the character is in lowercase then increment the countlow by 1.
  • Print the countlow.

Below is the implementation:

# Give the string as user input using the input() function and store it in a variable.
given_strng = input('Enter some random string = ')
# Take a variable to say countlow that stores the total number of lowercase characters present in the given string.
# Initialize the countlow to 0.
countlow = 0
# Traverse the given string using for loop.
for charact in given_strng:
    # Calculate the ASCII value of the character using the ord() function and
    # store it in a variable.
    ascValue = ord(charact)
    # If the ASCII values lie between 97 to 122 then the character is in lowercase.
    if(ascValue >= 97 and ascValue <= 122):
        # If the character is in lowercase then increment the countlow by 1.
        countlow = countlow+1
# Print the countlow.
print(
    'The total number of lower case letters present in the given string [', given_strng, '] = ', countlow)

Output:

Enter some random string = btechGeeks
The total number of lower case letters present in the given string [ btechGeeks ] = 9

Related Programs: