Home > Mobile >  How do we combine two activities - say one, remove numbers, special characters and two, convert uppe
How do we combine two activities - say one, remove numbers, special characters and two, convert uppe

Time:08-03

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))

CodePudding user response:

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))
  • Related