String to valid Python identifier

Дамјан Георгиевски gdamjan at
Sat Aug 8 14:25:40 CEST 2009

>> 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 _ ?
> I think this is fairly underspecified as a problem statement. A
> solution that would meet your specification would be

True, I was thinking that if there was an obvious solution I'd get the 
answer right away.. so it seems there's not.

I did try the string.maketrans way... it was a bit ugly so I decided to 
ask here if I'm missing something... maybe I'll try maketrans again.

by "convert" I didn't mean a full transformation... I was more hoping 
for the least intrusive transformation that would still leave 
discernible resemblance of the original string. 

дамјан ( )

When you do things right, people won't be sure if you did anything at 

More information about the Python-list mailing list