Arrange string characters such that lowercase letters should come first

1. Introduction

This blog post will focus on creating a Python program to arrange the characters of a string so that all lowercase letters come first, followed by uppercase letters. This task demonstrates string manipulation and sorting techniques in Python, which are fundamental skills in programming.

2. Program Steps

1. Create a function that takes a string as input.

2. Separate the string into lowercase and uppercase characters.

3. Concatenate the lowercase characters and uppercase characters.

4. Return or print the newly arranged string.

3. Code Program


def arrange_string(input_string):
    # Step 2: Separate lowercase and uppercase characters
    lower_case = ''
    upper_case = ''

    for char in input_string:
        if char.islower():
            lower_case += char
        elif char.isupper():
            upper_case += char

    # Step 3: Concatenate lowercase with uppercase characters
    arranged_string = lower_case + upper_case

    # Step 4: Return the arranged string
    return arranged_string

# Example usage
input_str = "PyThOnPrOgRaMmInG"
result = arrange_string(input_str)
print(f"Arranged string: {result}")

Output:

For the input string 'PyThOnPrOgRaMmInG', the output will be yhnmngrPOThOMRIG.

Explanation:

1. The function arrange_string is defined to accept a string as its argument.

2. It initializes two empty strings, lower_case and upper_case, to hold the lowercase and uppercase characters, respectively.

3. The function iterates over each character in input_string. If a character is lowercase (checked using char.islower()), it's added to lower_case. If it's uppercase (checked using char.isupper()), it's added to upper_case.

4. After the loop, the lowercase and uppercase strings are concatenated, with the lowercase string first, to form the arranged_string.

5. This newly arranged string is then returned.

This program effectively demonstrates how to manipulate and arrange the characters in a string based on their case in Python.


Comments