From c0d88d7f71b0234740696cbe48d7ed88900261c1 Mon Sep 17 00:00:00 2001 From: Frank Schmitt Date: Fri, 6 Nov 2020 18:09:12 +0100 Subject: [PATCH] Fix handling of non ascii characters in swap case (fixes: #3847) (#3848) * #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 Co-authored-by: Dhruv Manilawala --- strings/swap_case.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/strings/swap_case.py b/strings/swap_case.py index 71e8aeb3a..107fda4b5 100644 --- a/strings/swap_case.py +++ b/strings/swap_case.py @@ -11,14 +11,9 @@ For example: GITHUB.COM/MAYUR200 """ -import re - -# This re.compile() function saves the pattern from 'a' to 'z' and 'A' to 'Z' -# into 'regexp' variable -regexp = re.compile("[^a-zA-Z]+") -def swap_case(sentence): +def swap_case(sentence: str) -> str: """ This function will convert all lowercase letters to uppercase letters and vice versa. @@ -30,13 +25,13 @@ def swap_case(sentence): for char in sentence: if char.isupper(): new_string += char.lower() - if char.islower(): + elif char.islower(): new_string += char.upper() - if regexp.search(char): + else: new_string += char return new_string if __name__ == "__main__": - print(swap_case(input("Please input sentence:"))) + print(swap_case(input("Please input sentence: ")))