[Tutor] Stripping punctuation

Alan Gauld alan.gauld at btinternet.com
Fri May 30 01:27:50 CEST 2008


"W W" <srilyk at gmail.com> wrote

> Is there a faster way to strip punctuation from a string than this?

Yes

> from string import ascii_letters
> 
> def strip_punctuation(mystring):
>    newstring = ''
>    for x in mystring:
>        if x in ascii_letters:
>            newstring += x

This is very slow since it creates a new string with each 
addition, it doesn't just append a character at the end as 
you might think! It would be faster to add the chars to a 
list then use string join() at the end. But see below...

>        else:
>            pass

The else/pass is redundant you could remove it.

However, it's much easier to do a replacement operation and 
especially using a regex of all the non ascii characters - or 
whatever you define to be punctuation...

That way you can do a single operation on the entire 
string which will all be coded in optimised C rather than
clunking through, letter by letter in Python.

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld



More information about the Tutor mailing list