import re #first argument - string to be stripped #second argument - char or default " " def customStrip(string: str, stripChar: str="\s")->str: ''' Removes specified character from the beginning and end of a given string arguments: string: str stripChar: str returns: string ''' if stripChar == "" or stripChar == " ": stripChar = "\s" patternString = r"({}+)".format(stripChar) actions = ("^" + patternString,patternString + "$") for p in actions: pattern = re.compile(p) string = pattern.sub("",string) return string texts = [" aa dfgfhjgs, afktgrg aaa ","aaaaaaaaaaaaaaaajfdgf fdfgjkerturt aaaaa"] for text in texts: st = customStrip(text, "a") print("The text \n{}\n has {}characters.\nAfter strip \n{} \n====\n it now has {}".format(text,len(text),st,len(st))) print("-------------------------------------------")
