String to valid Python identifier

MRAB python at mrabarnett.plus.com
Wed Aug 5 20:26:22 EDT 2009


Дамјан Георгиевски wrote:
> Is there any easy function in the stdlib to convert any random string in 
> a valid Python identifier .. possibly by replacing non-valid characters 
> with _ ?
> 
> Python 2.x only, so no need to do Unicode.
> 
Lookup the 'maketrans' function in the 'string' module and the
'translate' method of the 'str' class.



More information about the Python-list mailing list