Toggle Each Character in a String Using Python



In this problem, we are given a string, and the task is to toggle the case of each character in the string. This means that all lowercase letters should be converted to uppercase, and all uppercase letters should be converted to lowercase. In this article, we are going to explore different approaches to toggling each character in a string using Python.

Let's look at the simple example, where we are going to provide input as "TutorialsPoint". Then output is like "tUTORIALSpOINT" In this case each character is toggled. Such that Uppercase letters become lowercase and lowercase becomes uppercase.

Below are different approaches to Toggle Each Character in a String

  • Using the Brute Force Approach
  • Using the Built-in swapcase() Function
  • Using the map() Function

Using the Brute Force Approach

This is the simplest and direct approach to toggling the case of each character in a string. In this approach, we traverse through each character of the string and manually check if it is uppercase or lowercase. If the character of the string is uppercase then convert it to lowercase and if the character is uppercase convert it to lowercase.

Steps for Implementation

  • Initialize an empty string to store the toggled characters.
  • Traverse through the given string.
  • If a character is lowercase, convert it to uppercase.
  • If a character is uppercase, convert it to lowercase.
  • Append each modified character to the result string.
  • Print the final toggled string.
Code Implementation
def toggle_case_brute_force(s):
    result = ""
    for char in s:
        if char.islower():
            result += char.upper()
        elif char.isupper():
            result += char.lower()
        else:
            result += char
    return result
input_string = "anshuayush"
print("Toggled String:", toggle_case_brute_force(input_string))
Output
Toggled String: ANSHUAYUSH

Using the Built-in swapcase() Function

Python has a built-in method swapcase() that automatically toggles the case of each character in a string. This is the simplest and most efficient approach. It directly toggles each character of the string. We can convert lowercase letters to uppercase and uppercase letters to lowercase using swapcase() function directly.

Steps for Implementation

  • First call swapcase() function on the string you want to modify.
  • Store the result in a variable.
  • Return or print the modified string.
Code Implementation
def toggle_case_using_swapcase(s):
    return s.swapcase()
input_string = "AnshuAyush"
print("Toggled String:", toggle_case_using_swapcase(input_string))
Output:
Toggled String: aNSHUaYUSH

Using the map() Function

In this approach, we use the map() function to convert uppercase to lowercase and lowercase to uppercase. This approach uses Python's map() function to apply case conversion to each character.

Steps for Implementation:

  • Use map() to iterate over the string.
  • Apply a lambda function to toggle the case of each character.
  • Convert the result to a string using join().
  • Return the final string.
Code Implementation
def toggle_case_using_map(s):
    return "".join(map(lambda char: char.upper() if char.islower() else char.lower(), s))
input_string = "AnShUaYUsh"
print("Toggled String:", toggle_case_using_map(input_string))
Output
Toggled String: aNSHuAyuSH
Updated on: 2025-02-04T16:17:21+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements