未验证 提交 c0d88d7f 编写于 作者: F Frank Schmitt 提交者: GitHub

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: NFrank Schmitt <frankschmitt@gmx.de>
Co-authored-by: NDhruv Manilawala <dhruvmanila@gmail.com>
上级 c8db6a20
......@@ -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: ")))
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册