Program to Find the First Small Letter in a Given String

Python Program to Find the First Small Letter in a Given String

In the previous article, we have discussed Python Program to Find the First Capital Letter in a Given String

Given a string and the task is to find the first small letter in a given string in python.

Examples:

Example1:

Input:

Given String = "GOOD morning Btechgeeks"

Output:

The given String's first small letter =  m

Example2:

Input:

Given String =  "hello this is btechgeeks"

Output:

The given String's first small letter =  h

Program to Find the First Small Letter in a Given String in Python

Below are the ways to find the first small letter in a given string in python:

Method #1: Using For Loop (Static Input)

Approach:

  • Give the string as static input and store it in a variable.
  • Take a new empty string and store it in another variable.
  • Calculate the length of the given string using the len() function and store it in another variable.
  • Loop till the length of the given string using the for loop.
  • Check if the character which is present at the iterator value of the given string is greater than or equal to ‘a’ and less than or equal to ‘z’ using the if conditional statement.
  • If the statement is true, then add the character which is present at the iterator value of the given string to the above-initialized new string.
  • Break the statement.
  • If the statement is false, then continue.
  • Print the given string’s first small letter.
  • The Exit of the Program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gven_str = "GOOD morning Btechgeeks"
# Take a new empty string and store it in another variable.
fstsmall_lettr = ' '
# Calculate the length of the given string using the len() function and store it in
# another variable.
str_lengt = len(gven_str)
# Loop till the length of the given string using the for loop.
for itr in range(str_lengt):
   # Check if the character which is present at the iterator value of the given string
   # is greater than or equal to 'a' and less than or equal to 'z' using the if
   # conditional statement.
    if gven_str[itr] >= 'a' and gven_str[itr] <= 'z':
       # If the statement is true, then add the character which is present at the iterator
       # value of the given string to the above-initialized new string.
        fstsmall_lettr = gven_str[itr]
    # Break the statement.
        break
    else:
      # If the statement is false, then continue.
        continue
# Print the given string's first small letter.
print("The given String's first small letter = ", fstsmall_lettr)

Output:

The given String's first small letter =  m

Method #2: Using For loop (User Input)

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Take a new empty string and store it in another variable.
  • Calculate the length of the given string using the len() function and store it in another variable.
  • Loop till the length of the given string using the for loop.
  • Check if the character which is present at the iterator value of the given string is greater than or equal to ‘a’ and less than or equal to ‘z’ using the if conditional statement.
  • If the statement is true, then add the character which is present at the iterator value of the given string to the above-initialized new string.
  • Break the statement.
  • If the statement is false, then continue.
  • Print the given string’s first small letter.
  • The Exit of the Program.

Below is the implementation:

# Give the string as user input using the input() function and 
# store it in a variable.
gven_str = input("Enter some Random String = ")
# Take a new empty string and store it in another variable.
fstsmall_lettr = ' '
# Calculate the length of the given string using the len() function and store it in
# another variable.
str_lengt = len(gven_str)
# Loop till the length of the given string using the for loop.
for itr in range(str_lengt):
   # Check if the character which is present at the iterator value of the given string
   # is greater than or equal to 'a' and less than or equal to 'z' using the if
   # conditional statement.
    if gven_str[itr] >= 'a' and gven_str[itr] <= 'z':
       # If the statement is true, then add the character which is present at the iterator
       # value of the given string to the above-initialized new string.
        fstsmall_lettr = gven_str[itr]
    # Break the statement.
        break
    else:
      # If the statement is false, then continue.
        continue
# Print the given string's first small letter.
print("The given String's first small letter = ", fstsmall_lettr)

Output:

Enter some Random String = hello this is btechgeeks
The given String's first small letter = h

If you wanna write simple python programs as a part of your coding practice refer to numerous Simple Python Program Examples existing and learn the approach used.