0

I want to find the number of sentences of a text.

so I use:

n_sentence = text.count('.') + text.count('!') + text.count('?')

It's working and it's really ugly.

I've read the .count method but I can't find how to add multiple object to be counted in.

Or maybe the method re.findall is more suited? (I've miss something because I can't make it work.)

Thanks in advance,

2 Answers 2

1

Try .split() instead. And if you need to split multiple characters use regular expressions re.split() provided with characters given in specification for readability.c in pset2.

1

ugly's in the eyes of the beholder. What could be more explicit, more readable, simpler than that? There doesn't seem to be (read: I couldn't find) a python string method that will do it in one pass. Any other approach would require a loop (eg. count on multiple objects) or an additional library (eg. re for findall) or a new function etc. etc. More is not necessarily better.

There is a Code Review community on stack. Perhaps have a browse through those posts, or pose this question to that community.

(FWIW: that's how I did it; I think it's beautiful :)

1
  • Ok thank. Just want to avoid all these iterations.
    – saquiel
    Commented Jan 16, 2020 at 16:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .