Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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  print clean(s1)+'_'#the _ is there so I can see the end of the string 

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 ?

like image 915
George Profenza Avatar asked Jul 21 '10 19:07

George Profenza


People also ask

How do you convert a string to a variable name in Python?

String Into Variable Name in Python Using the vars() Function. Instead of using the locals() and the globals() function to convert a string to a variable name in python, we can also use the vars() function. The vars() function, when executed in the global scope, behaves just like the globals() function.

Is string a valid variable name in Python?

You can check whether a string is a valid identifier or not with the method isidentifier() of the string str . It returns True if the string is a valid identifier and False if it is not.

How do you save a string as a variable in Python?

Declaring strings as variables can make it easier for us to work with strings throughout our Python programs. To store a string inside a variable, we need to assign a variable to a string. In this case let's declare my_str as our variable: my_str = "Sammy likes declaring strings."

How do you write a valid variable name?

No other characters are permitted in the variable name. Specifically, spaces are not permitted in the variable names, as variable name must be a single word. Variable name may not start with a digit or underscore, and may not end with an underscore. Double underscores are not permitted in variable name.


2 Answers

Well, I'd like to best Triptych's solution with ... a one-liner!

>>> def clean(varStr): return re.sub('\W|^(?=\d)','_', varStr) ...  >>> clean('32v2 g #Gmw845h$W b53wi ') '_32v2_g__Gmw845h_W_b53wi_' 

This substitution replaces any non-variable appropriate character with underscore and inserts underscore in front if the string starts with a digit. IMO, 'name/with/slashes' looks better as variable name name_with_slashes than as namewithslashes.

like image 165
Nas Banov Avatar answered Sep 22 '22 22:09

Nas Banov


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' 
like image 22
Kenan Banks Avatar answered Sep 18 '22 22:09

Kenan Banks