String to valid Python identifier

MRAB python at
Thu Aug 6 02:26:22 CEST 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