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 <frankschmitt@gmx.de>
Co-authored-by: Dhruv Manilawala <dhruvmanila@gmail.com>
This commit is contained in:
Frank Schmitt 2020-11-06 18:09:12 +01:00 committed by GitHub
parent c8db6a208b
commit c0d88d7f71
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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: ")))