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 ?
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.
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.
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."
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.
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
.
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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With