Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do we combine two activities – say one, remove numbers, special characters and two, convert uppercase to lowercase letters, and back

Typed code : How to combine the two parts in the code for 1 output >>

s= input()
   
def swap_case(s):
    word = []
    for char in s:
      if char.islower():
        word.append(char.upper())
      else:
        word.append(char.lower())
    str1 = ''.join(word)
    return str1
    
    import re
    new_string = re.sub('[^A-Za-z]+', '', s)
    return new_string

print(swap_case(s))

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can first remove the characters that you want, and then do the swapping.

import re

s = input()

def swap_case(str):
    word = []
    for char in re.sub('[^A-Za-z]+', '', str):
        if char.islower():
            word.append(char.upper())
        else:
            word.append(char.lower())
    return ''.join(word)

print(swap_case(s))

Or in short:

import re

s = input()

def swap_case(str):
    return re.sub('[^A-Za-z]+', '', str).swapcase()


print(swap_case(s))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading