pythonvalidationstringvariables

How do I convert a string to a valid variable name in Python?


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?


Solution

  • According to Python, an identifier is a letter or underscore, followed by an unlimited string of letters, numbers, and underscores:

    import re
    
    def clean(s):
    
       # Remove invalid characters
       s = re.sub('[^0-9a-zA-Z_]', '', s)
    
       # Remove leading characters until we find a letter or underscore
       s = re.sub('^[^a-zA-Z_]+', '', s)
    
       return s
    

    Use like this:

    >>> clean(' 32v2 g #Gmw845h$W b53wi ')
    'v2gGmw845hWb53wi'