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.
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)
If the user inputs "Hello World", the output will be:
The modified string with alternating caps is: HeLlO WoRl