mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-30 16:31:08 +00:00
c0d88d7f71
* #3847 fix handling of non-ASCII characters in swap_case * #3847 remove unused regex * Fix formatting (with black) Fixes: #3847 * Add type hints for `swap_case` function Co-authored-by: Frank Schmitt <frankschmitt@gmx.de> Co-authored-by: Dhruv Manilawala <dhruvmanila@gmail.com>
38 lines
895 B
Python
38 lines
895 B
Python
"""
|
|
This algorithm helps you to swap cases.
|
|
|
|
User will give input and then program will perform swap cases.
|
|
|
|
In other words, convert all lowercase letters to uppercase letters and vice versa.
|
|
For example:
|
|
1. Please input sentence: Algorithm.Python@89
|
|
aLGORITHM.pYTHON@89
|
|
2. Please input sentence: github.com/mayur200
|
|
GITHUB.COM/MAYUR200
|
|
|
|
"""
|
|
|
|
|
|
def swap_case(sentence: str) -> str:
|
|
"""
|
|
This function will convert all lowercase letters to uppercase letters
|
|
and vice versa.
|
|
|
|
>>> swap_case('Algorithm.Python@89')
|
|
'aLGORITHM.pYTHON@89'
|
|
"""
|
|
new_string = ""
|
|
for char in sentence:
|
|
if char.isupper():
|
|
new_string += char.lower()
|
|
elif char.islower():
|
|
new_string += char.upper()
|
|
else:
|
|
new_string += char
|
|
|
|
return new_string
|
|
|
|
|
|
if __name__ == "__main__":
|
|
print(swap_case(input("Please input sentence: ")))
|