String to valid Python identifier

Дамјан Георгиевски gdamjan at gmail.com
Sat Aug 8 08:25:40 EDT 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.


ps.
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. 



-- 
дамјан ( http://softver.org.mk/damjan/ )

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



More information about the Python-list mailing list