I'm trying to recreate the strip()
function of python using Regex. It's the last practice problem from Automate the Boring Stuff with Python. Here's my code:
import re
stripChar = input('Enter character to strip: ')
context = input('Enter string to strip: ')
stripContext = None
def strip(char, string):
if stripChar == "":
regsp = re.compile(r'^\s+|\s+$')
stripContext = regsp.sub("", context)
return stripContext
else:
stripContext = re.sub(r'^(char)+', "", string)
return stripContext
print(strip(stripChar, context))
In line 16, if I replace (char) with any random character, the program is working. However, I can't seem to make a custom variable work there. What am I doing wrong there?
Edit: Stack is saying it's a duplicate of this question. It's not because it' s purely around Regex not only Python.