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

In the previous article, we have discussed Python Program to Convert Alternate Characters to Capital Letters

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

Examples:

Example1:

Input:

Given String = "hello this is Btechgeeks"

Output:

The given String's first capital letter =  B

Example2:

Input:

Given String = "good Morning Btechgeeks"

Output:

The given String's first capital letter =  M

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

Below are the ways to find the first capital 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 capital letter.
  • The Exit of the Program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gven_str = "hello this is Btechgeeks"
# Take a new empty string and store it in another variable.
fstcapitl_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.
        fstcapitl_lettr = gven_str[itr]
    # Break the statement.
        break
    else:
      # If the statement is false, then continue.
        continue
# Print the given string's first capital letter.
print("The given String's first capital letter = ", fstcapitl_lettr)

Output:

The given String's first capital letter =  B

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 capital 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.
fstcapitl_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.
        fstcapitl_lettr = gven_str[itr]
    # Break the statement.
        break
    else:
      # If the statement is false, then continue.
        continue
# Print the given string's first capital letter.
print("The given String's first capital letter = ", fstcapitl_lettr)

Output:

Enter some Random String = good Morning Btechgeeks
The given String's first capital letter = M

Find a comprehensive collection of Examples of Python Programs ranging from simple ones to complex ones to guide you throughout your coding journey.