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

Viewed 14120

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?

4 Answers

You can use the built in func:str.isidentifier() in combination with filter(). This requires no imports such as re and works by iterating over each character and returning it if its an identifier. Then you just do a ''.join to convert the array to a string again.

s1 = 'name/with/slashes'
s2 = 'name '

def clean(s):
    s = ''.join(filter(str.isidentifier, s))
    return s

print f'{clean(s1)}_' #the _ is there so I can see the end of the string

EDIT:

If, like Hans Bouwmeester in the replies, want numeric values to be included as well, you can create a lambda which uses both the isIdentifier and the isdecimal functions to check the characters. Obviously this can be expanded as far as you want to take it. Code:

s1 = 'name/with/slashes'
s2 = 'name i2, i3    '
s3 = 'epng2 0-2g [ q4o 2-=2 t1  l32!@#$%*(vqv[r 0-34 2]] '

def clean(s):
    s = ''.join(filter( 
        lambda c: str.isidentifier(c) or str.isdecimal(c), s))
    return s
#the _ is there so I can see the end of the string
print(f'{ clean(s1) }_')
print(f'{ clean(s2) }_')
print(f'{ clean(s3) }_')

Gives :

namewithslashes_
namei2i3_
epng202gq4o22t1l32vqvr0342_
Related