[Tutor] how to use replace() from a list

Alan Gauld alan.gauld at btinternet.com
Sat Jun 16 23:30:17 CEST 2007

"Norman Khine" <norman at khine.net> wrote 

>    name = title.lower().replace('/', '_').replace('?',
> '_').replace('.', '')
>    return '_'.join(name.split())
> Is there a way to have just one replace and so that:

Check out the string.maketrans() function and 
the translate method.

Basically you do something like:

table = string.maketrans('/?_','___')
title = title.translate(table)

Now I'm not sure how it will react to the translation mapping 
all three chars to the same result char, but it should work...


Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list