mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-01-18 08:17:01 +00:00
* #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:
parent
c8db6a208b
commit
c0d88d7f71
|
@ -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: ")))
|
||||
|
|
Loading…
Reference in New Issue
Block a user