Program to Convert Alternate Characters to Capital Letters

Python Program to Convert Alternate Characters to Capital Letters

In the previous article, we have discussed Python Program to Check Two Matrix are Equal or Not

Given a string, and the task is to convert the alternate characters to capital letters for a given string in python

Examples:

Example1:

Input:

Given String = "good morning btechgeeks"

Output:

The given string after converting alternate characters into capital letters :
GoOd MoRnInG bTeChGeEkS

Example2:

Input:

Given String = "hello this is btechgeeks"

Output:

The given string after converting alternate characters into capital letters :
HeLlO tHiS iS bTeChGeEkS

Program to Convert Alternate Characters to Capital Letters in Python

Below are the ways to convert the alternate characters to capital letters for 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 variable say k and initialize its value to zero.
  • 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 above-initialized k value modulus 2 is equal to zero using the if conditional statement.
  • If the Statement is true, 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 it is true, calculate the ASCII value of the character of the given string which is present at the iterator value index using the ord() function, and add 32 to it.
  • Find the character of the above ASCII value using the chr() function.
  • Concatenate the above character to the modified string.
  • If the statement is False, then concatenate the character which is present at the iterator value of the given string to the modified string.
  • Else, check if the character which is present at the iterator value is greater than or equal to ‘a’ and less than or equal to ‘z’ using the if conditional statement.
  • If it is true, calculate the ASCII value of the character of the given string which is present at the iterator value index using the ord() function, and subtract 32 from it.
  • Find the character of the above ASCII value using the chr() function.
  • Concatenate the above character to the modified string.
  • If the statement is False, then concatenate the character which is present at the iterator value of the given string to the modified string.
  • Check if the character present at the iterator value is equal to space using the if conditional statement.
  • If it is true, then continue.
  • Increment the value of k by 1 and store it in the same variable k.
  • Print the modified string to get the given string after converting alternate characters into capital letters.
  • 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 variable say k and initialize its value to zero.
k = 0
# Take a new empty string and store it in another variable.
modified_strng = ""
# 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 above-initialized k value modulus 2 is equal to zero using the if
     # conditional statement.
    if k % 2 == 1:
      # If the Statement is true, 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 it is true, calculate the ASCII value of the character of the given string which is
           # present at the iterator value index using the ord() function, and add 32 to it
           # Find the character of the above ASCII value using the chr() function.
            charctr = chr(ord(gven_str[itr])+32)
    # Concatenate the above character to the modified string.
            modified_strng = modified_strng+charctr
        else:
           # If the statement is False, then concatenate the character which is present at the
           # iterator value of the given string to the modified string.
            modified_strng = modified_strng+gven_str[itr]
    else:
      # Else, 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 it is true, calculate the ASCII value of the character of the given string which
          # is present at the iterator value index using the ord() function, and subtract
          # 32 from it.
          # Find the character of the above ASCII value using the chr() function.
            charctr = chr(ord(gven_str[itr])-32)
          # Concatenate the above character to the modified string.
            modified_strng = modified_strng+charctr
        else:
         # If the statement is False, then concatenate the character which is present at the
         # iterator value of the given string to the modified string.
            modified_strng = modified_strng+gven_str[itr]
   # Check if the character present at the iterator value is equal to space using the if
   # conditional statement.

    if gven_str[itr] == ' ':
      # If it is true, then continue.
        continue
    # Increment the value of k by 1 and store it in the same variable k.
    k = k+1
# Print the modified string to get the given string after converting alternate characters
# into capital letters.
print("The given string after converting alternate characters into capital letters :")
print(modified_strng)

Output:

The given string after converting alternate characters into capital letters :
GoOd MoRnInG bTeChGeEkS

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 variable say k and initialize its value to zero.
  • 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 above-initialized k value modulus 2 is equal to zero using the if conditional statement.
  • If the Statement is true, Check if the character which is present at the iterator value is greater than or equal to ‘A’ and less than or equal to ‘Z’ using the if conditional statement.
  • If it is true, calculate the ASCII value of the character of the given string which is present at the iterator value index using the ord() function, and add 32 to it.
  • Find the character of the above ASCII value using the chr() function.
  • Concatenate the above character to the modified string.
  • If the statement is False, then concatenate the character which is present at the iterator value of the given string to the modified string.
  • Else, check if the character which is present at the iterator value is greater than or equal to ‘a’ and less than or equal to ‘z’ using the if conditional statement.
  • If it is true, calculate the ASCII value of the character of the given string which is present at the iterator value index using the ord() function, and subtract 32 from it.
  • Find the character of the above ASCII value using the chr() function.
  • Concatenate the above character to the modified string.
  • If the statement is False, then concatenate the character which is present at the iterator value of the given string to the modified string.
  • Check if the character present at the iterator value is equal to space using the if conditional statement.
  • If it is true, then continue.
  • Increment the value of k by 1 and store it in the same variable k.
  • Print the modified string to get the given string after converting alternate characters into capital letters.
  • 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 variable say k and initialize its value to zero.
k = 0
# Take a new empty string and store it in another variable.
modified_strng = ""
# 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 above-initialized k value modulus 2 is equal to zero using the if
     # conditional statement.
    if k % 2 == 1:
      # If the Statement is true, 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 it is true, calculate the ASCII value of the character of the given string which is
           # present at the iterator value index using the ord() function, and add 32 to it
           # Find the character of the above ASCII value using the chr() function.
            charctr = chr(ord(gven_str[itr])+32)
    # Concatenate the above character to the modified string.
            modified_strng = modified_strng+charctr
        else:
           # If the statement is False, then concatenate the character which is present at the
           # iterator value of the given string to the modified string.
            modified_strng = modified_strng+gven_str[itr]
    else:
      # Else, 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 it is true, calculate the ASCII value of the character of the given string which
          # is present at the iterator value index using the ord() function, and subtract
          # 32 from it.
          # Find the character of the above ASCII value using the chr() function.
            charctr = chr(ord(gven_str[itr])-32)
  # Concatenate the above character to the modified string.
            modified_strng = modified_strng+charctr
        else:
         # If the statement is False, then concatenate the character which is present at the
         # iterator value of the given string to the modified string.
            modified_strng = modified_strng+gven_str[itr]
   # Check if the character present at the iterator value is equal to space using the if
   # conditional statement.

    if gven_str[itr] == ' ':
      # If it is true, then continue.
        continue
    # Increment the value of k by 1 and store it in the same variable k.
    k = k+1
# Print the modified string to get the given string after converting alternate characters
# into capital letters.
print("The given string after converting alternate characters into capital letters :")
print(modified_strng)

Output:

Enter some Random String = hello this is btechgeeks
The given string after converting alternate characters into capital letters :
HeLlO tHiS iS bTeChGeEkS

Find the best practical and ready-to-use Python Programming Examples that you can simply run on a variety of platforms and never stop learning.