I need to convert an arbitrary string to a string that is a valid variable name in Python.
Here’s a very basic example:
s1 = 'name/with/slashes'
s2 = 'name '
def clean(s):
s = s.replace('/', '')
s = s.strip()
return s
# the _ is there so I can see the end of the string
print clean(s1) + '_'
That is a very naive approach. I need to check if the string contains invalid variable name characters and replace them with ”
What would be a pythonic way to do this?
According to Python, an identifier is a letter or underscore, followed by an unlimited string of letters, numbers, and underscores:
Use like this: