[Tutor] Iteration issues

Neil Cerutti neilc at norwich.edu
Fri May 11 09:43:59 EDT 2018


On 2018-05-10, Albert-Jan Roskam <sjeik_appie at hotmail.com> wrote:
> If a punctuation symbol is in your string:  Replace that symbol
> with an empty string.
>
>=>>> maybe something like
>
> import re
> no_interpunction = re.sub("[%s]" % re.escape(string.punctuation), '', sentence)

str.translate can be used instead of re.

# Compute only once somewhere
punctuation_removal_table = str.maketrans({c: None for c in string.punctuation})


    no_interpunction = sentence.translate(punctuation_removal_table)

Unfortunately you'll remove all the apostrophes and dashes, both
of which could be considered parts of words.

-- 
Neil Cerutti



More information about the Tutor mailing list