Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program to Toggle Each Character in a String
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 a simple example, where we provide input as "TutorialsPoint". The output would be "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 is uppercase, we convert it to lowercase, and if the character is lowercase, we convert it to uppercase.
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.
Example
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 = "TutorialsPoint"
print("Original String:", input_string)
print("Toggled String:", toggle_case_brute_force(input_string))
The output of the above code is ?
Original String: TutorialsPoint Toggled String: tUTORIALSpOINT
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 by converting lowercase letters to uppercase and uppercase letters to lowercase.
Steps for Implementation
- Call the
swapcase()function on the string you want to modify. - Store the result in a variable.
- Return or print the modified string.
Example
def toggle_case_using_swapcase(s):
return s.swapcase()
input_string = "TutorialsPoint"
print("Original String:", input_string)
print("Toggled String:", toggle_case_using_swapcase(input_string))
The output of the above code is ?
Original String: TutorialsPoint Toggled String: tUTORIALSpOINT
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.
Example
def toggle_case_using_map(s):
return "".join(map(lambda char: char.upper() if char.islower() else char.lower(), s))
input_string = "TutorialsPoint"
print("Original String:", input_string)
print("Toggled String:", toggle_case_using_map(input_string))
The output of the above code is ?
Original String: TutorialsPoint Toggled String: tUTORIALSpOINT
Comparison
| Method | Time Complexity | Best For |
|---|---|---|
| Brute Force | O(n) | Learning algorithm logic |
| swapcase() | O(n) | Clean, readable code |
| map() Function | O(n) | Functional programming style |
Conclusion
The swapcase() method is the most efficient and readable approach for toggling character cases in Python. For learning purposes, the brute force approach helps understand the underlying logic, while map() offers a functional programming alternative.
