Skip to content

Fix handling of non ascii characters in swap case (fixes: #3847) #3848

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions strings/swap_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,9 @@
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.
Expand All @@ -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: ")))