Write a python program to return a string such that the string is arranged from lowercase letters and then uppercase letters

1. Introduction

In this blog post, we'll explore how to write a Python program that rearranges a given string so that all lowercase letters are placed before uppercase letters. This task demonstrates string manipulation and character classification, essential skills in Python programming.

2. Program Steps

1. Define a function that takes a string as an argument.

2. Iterate over the string, categorizing each character as lowercase or uppercase.

3. Concatenate all lowercase characters first, followed by all uppercase characters.

4. Return or print the rearranged string.

3. Code Program


def rearrange_string(input_string):
    # Initialize strings for lowercase and uppercase characters
    lower_case_part = ""
    upper_case_part = ""

    # Iterate over the string and categorize characters
    for char in input_string:
        if char.islower():
            lower_case_part += char
        elif char.isupper():
            upper_case_part += char

    # Concatenate lowercase and uppercase parts
    rearranged_string = lower_case_part + upper_case_part

    # Return the rearranged string
    return rearranged_string

# Example usage
input_str = "PyThoNProGRaMMinG"
result = rearrange_string(input_str)
print(f"Rearranged string: {result}")

Output:

For the input string 'PyThoNProGRaMMinG', the output will be yhooraingPTNPRGMM.

Explanation:

1. The function rearrange_string takes a string input_string as an input.

2. Two empty strings, lower_case_part and upper_case_part, are initialized to store the lowercase and uppercase characters, respectively.

3. The function iterates over each character in input_string. The islower() and isupper() methods are used to check if a character is lowercase or uppercase, respectively.

4. Lowercase characters are concatenated to lower_case_part, and uppercase characters are concatenated to upper_case_part.

5. The rearranged_string is created by concatenating lower_case_part and upper_case_part, ensuring that all lowercase letters come first.

6. This rearranged string is then returned. For the input 'PyThoNProGRaMMinG', the lowercase characters yhooraing are followed by the uppercase characters PTNPRGMM.

This program effectively demonstrates string manipulation in Python, specifically how to categorize and rearrange characters based on their case.


Comments