Skip to content Skip to sidebar Skip to footer

How To Count Paragraphs?

How do you counts paragraphs from a file after string.read()? I have no Idea where to start. THE Mississippi is well worth reading about. It is not a commonplace river, but on the

Solution 1:

i personnaly use \n, here's how i do it

defpar_counter(txt):
    txt = txt.strip() # to remove empty lines at the beginning and endreturnlen([i for i in txt if i == '\n'])

if don't want to use \n to count and use empty lines (between paragraphs) instead, you can do this :

defpar_counter(txt):
    lines = txt.strip().splitlines() # to remove empty lines at the beginning and end   
    emptylines = [e for e in lines if e.strip() == ''] # find empty lines (line between paragraph and another)# return nb lines + 1 because if there are N empty lines, then we have N+1 paragraphsreturnlen(emptylines) + 1

Post a Comment for "How To Count Paragraphs?"