Text Processing - Python Programming Exercise

In this exercise, you will develop a Python program that prompts the user for a string and performs several transformations. The program will 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), and display odd letters in uppercase and even letters in lowercase. This exercise is perfect for practicing string manipulation and loops in Python. By implementing these transformations, you will gain hands-on experience in handling user input and output in Python. This exercise not only reinforces your understanding of string manipulation but also helps you develop efficient coding practices for managing user interactions.

 Category

Arrays, Lists, and Strings

 Exercise

Text Processing

 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

 Copy 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

 More Python Programming Exercises of Arrays, Lists, and Strings

Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.

  •  Hierarchical Structures

    In this exercise, you will develop a Python program to store two pieces of data for a person: Name and Date of birth. The Date of birth must be a...

  •  Data Organization

    In this exercise, you will develop a Python program that prompts the user for 10 integer numbers (ranging from -1000 to 1000), sorts them, and displays them in sorted...

  •  Screen Buffer Using 2D Array

    In this exercise, you will develop a Python program that declares a 70x20 two-dimensional array of characters, "draws" 80 letters (X, for example) in random positions...

  •  2D Array: Display Circle

    In this exercise, you will develop a Python program that creates a 70x20 two-dimensional array of characters, "draws" a circle with a radius of 8 inside it, and displ...

  •  Software Applications

    In this exercise, you will develop a Python program that can store up to 1,000 records of software applications. For each application, you must keep the following dat...

  •  Exercise tasks

    In this exercise, you will develop a Python program that can store up to 2,000 "to-do tasks". For each task, it must keep the following data: Date (a set of 3 ...