Skip to content

Commit c0d88d7

Browse files
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 <[email protected]> Co-authored-by: Dhruv Manilawala <[email protected]>
1 parent c8db6a2 commit c0d88d7

File tree

1 file changed

+4
-9
lines changed

1 file changed

+4
-9
lines changed

strings/swap_case.py

+4-9
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,9 @@
1111
GITHUB.COM/MAYUR200
1212
1313
"""
14-
import re
1514

16-
# This re.compile() function saves the pattern from 'a' to 'z' and 'A' to 'Z'
17-
# into 'regexp' variable
18-
regexp = re.compile("[^a-zA-Z]+")
1915

20-
21-
def swap_case(sentence):
16+
def swap_case(sentence: str) -> str:
2217
"""
2318
This function will convert all lowercase letters to uppercase letters
2419
and vice versa.
@@ -30,13 +25,13 @@ def swap_case(sentence):
3025
for char in sentence:
3126
if char.isupper():
3227
new_string += char.lower()
33-
if char.islower():
28+
elif char.islower():
3429
new_string += char.upper()
35-
if regexp.search(char):
30+
else:
3631
new_string += char
3732

3833
return new_string
3934

4035

4136
if __name__ == "__main__":
42-
print(swap_case(input("Please input sentence:")))
37+
print(swap_case(input("Please input sentence: ")))

0 commit comments

Comments
 (0)