Alternate Capital Letters Program in Python

Answered using GPT-4o mini by AI Answer Generator

Question:

Program 5: “Alternate Capital Letters” The intent of this program is to allow a user to enter a string of at least 10 characters (can be a sentence), and then output a modified string with every second character capitalized. Use only a “for loop” to generate the modified string.

  1. The first output should be a program header as described above like: ########## Program #5 – Alternate Capital Letters ###########
  2. above
  3. a prompt to get the user input
  4. a for loop that constructs the modified string: The Output would look something like this: Hello World with alternating caps is HeLlO WoRlD
Answer:

Here's a Python program that meets the requirements you've outlined for "Alternate Capital Letters". The program prompts the user for a string of at least 10 characters, then constructs a modified string with every second character capitalized using a for loop.

# Program Header
print("########## Program #5 – Alternate Capital Letters ###########")

# Prompt for user input
user_input = input("Please enter a string of at least 10 characters: ")

# Check if the input is at least 10 characters long
if len(user_input) < 10:
    print("Error: The input must be at least 10 characters long.")
else:
    # Initialize an empty string for the modified output
    modified_string = ""

    # For loop to construct the modified string
    for i in range(len(user_input)):
        if i % 2 == 0:
            modified_string += user_input[i]  # Keep the character as is
        else:
            modified_string += user_input[i].upper()  # Capitalize the character

    # Output the modified string
    print("The modified string with alternating caps is:", modified_string)

Explanation:

  1. Program Header: The program starts by printing a header to indicate the program number and purpose.
  2. User Input: It prompts the user to enter a string and checks if the length is at least 10 characters.
  3. For Loop: The loop iterates through each character in the input string. If the index is even, it appends the character as is; if the index is odd, it appends the uppercase version of the character.
  4. Output: Finally, it prints the modified string with alternating capital letters.

Example Usage:

If the user inputs "Hello World", the output will be:

The modified string with alternating caps is: HeLlO WoRl