Removing unwanted characters from a string in Python

前端 未结 9 1897
暖寄归人
暖寄归人 2021-01-18 09:06

I have some strings that I want to delete some unwanted characters from them. For example: Adam\'sApple ----> AdamsApple.(case insensitive) Can someone help

9条回答
  •  囚心锁ツ
    2021-01-18 09:49

    An alternative that will take in a string and an array of unwanted chars

        # function that removes unwanted signs from str
        #Pass the string to the function and an array ofunwanted chars
    
    def removeSigns(str,arrayOfChars):
    
        charFound = False
    
        newstr = ""
    
        for letter in str:
            for char in arrayOfChars:
                if letter == char:
                    charFound = True
                    break
            if charFound == False:
                newstr += letter
            charFound = False
    
        return newstr
    

提交回复
热议问题