Objective
Develop a Python program that prompts the user for a string and:
- Replace all lowercase 'a' with uppercase 'A', except if they are preceded by a space
- Display the initials (first letter and those after a space)
- Display odd letters in uppercase and even letters in lowercase
The program must display all generated strings.
Example Python Exercise
Show Python Code
# Main program loop
while True:
user_input = input("Enter a string: ")
# Replace all lowercase 'a' with uppercase 'A', except if preceded by a space
modified_string_1 = ""
for i in range(len(user_input)):
if user_input[i] == 'a' and (i == 0 or user_input[i-1] != ' '):
modified_string_1 += 'A'
else:
modified_string_1 += user_input[i]
# Display initials (first letter and those after a space)
initials = ""
words = user_input.split()
for word in words:
initials += word[0]
# Display odd letters in uppercase and even letters in lowercase
modified_string_2 = ""
for i in range(len(user_input)):
if i % 2 == 0:
modified_string_2 += user_input[i].lower()
else:
modified_string_2 += user_input[i].upper()
# Display the results
print("Modified string (replace 'a' with 'A' except after space):", modified_string_1)
print("Initials:", initials)
print("Odd letters uppercase, even letters lowercase:", modified_string_2)
break
Output
Enter a string: bananas are amazing
Modified string (replace 'a' with 'A' except after space): bAnAnAs Are AmAzIng
Initials: baa
Odd letters uppercase, even letters lowercase: bAnAnAs ArE AmAzInG
Share this Python Exercise