-4

example how to count the word "paragraph" in the paragraph below..

A paragraph in Word is any text that ends with a hard return. You insert a hard return anytime you press the Enter key. Paragraph formatting lets you control the appearance if individual paragraphs. For example, you can change the alignment of text from left to center or the spacing between lines form single to double. You can indent paragraphs, number them, or add borders and shading to them.

Paragraph formatting is applied to an entire paragraph. All formatting for a paragraph is stored in the paragraph mark and carried to the next paragraph when you press the Enter key. You can copy paragraph formats from paragraph to paragraph and view formats through task panes.

Angel
  • 11
  • 1
  • 5
    possible duplicate of [Finding number of Substrings in a String](http://stackoverflow.com/questions/8899905/finding-number-of-substrings-in-a-string) – Adam Jul 15 '14 at 22:24
  • What have you tried so far and where are you getting stuck? Did you attempt to search before asking this question? – Matt Coubrough Jul 15 '14 at 22:30

3 Answers3

2

You want to use the count method on the input string, passing "paragraph" as the argument.

>>> text = """A paragraph in Word is any text that ends with a hard return. You insert a hard return anytime you press the Enter key. Paragraph formatting lets you control the appearance if individual paragraphs. For example, you can change the alignment of text from left to center or the spacing between lines form single to double. You can indent paragraphs, number them, or add borders and shading to them.
    Paragraph formatting is applied to an entire paragraph. All formatting for a paragraph is stored in the paragraph mark and carried to the next paragraph when you press the Enter key. You can copy paragraph formats from paragraph to paragraph and view formats through task panes."""

>>> text.count('paragraph') # case sensitive
10
>>> text.lower().count('paragraph') # case insensitive
12

As mentioned in the comments, you can use lower() to transform the text to be all lowercase. This will include instances of "paragraph" and "Paragraph" in the count.

okoboko
  • 4,332
  • 8
  • 40
  • 67
  • I didn't re-run it when I switched to triple quotes (""") to delimit the string (which includes the second paragraph). 10 and 12 are correct - edited the answer accordingly. – okoboko Jul 15 '14 at 22:46
0

I would do the following:

  1. Split into a list of words (although not totally necessary)
  2. Lowercase all the words
  3. Use count to count the number of instances
>>> s
'A paragraph in Word is any text that ends with a hard return. You insert a hard return anytime you press the Enter key. Paragraph formatting lets you control the appearance if individual paragraphs. For example, you can change the alignment of text from left to center or the spacing between lines form single to double. You can indent paragraphs, number them, or add borders and shading to them.\n\n    Paragraph formatting is applied to an entire paragraph. All formatting for a paragraph is stored in the paragraph mark and carried to the next paragraph when you press the Enter key. You can copy paragraph formats from paragraph to paragraph and view formats through task panes.'
>>> s.split()
['A', 'paragraph', 'in', 'Word', 'is', 'any', 'text', 'that', 'ends', 'with', 'a', 'hard', 'return.', 'You', 'insert', 'a', 'hard', 'return', 'anytime', 'you', 'press', 'the', 'Enter', 'key.', 'Paragraph', 'formatting', 'lets', 'you', 'control', 'the', 'appearance', 'if', 'individual', 'paragraphs.', 'For', 'example,', 'you', 'can', 'change', 'the', 'alignment', 'of', 'text', 'from', 'left', 'to', 'center', 'or', 'the', 'spacing', 'between', 'lines', 'form', 'single', 'to', 'double.', 'You', 'can', 'indent', 'paragraphs,', 'number', 'them,', 'or', 'add', 'borders', 'and', 'shading', 'to', 'them.', 'Paragraph', 'formatting', 'is', 'applied', 'to', 'an', 'entire', 'paragraph.', 'All', 'formatting', 'for', 'a', 'paragraph', 'is', 'stored', 'in', 'the', 'paragraph', 'mark', 'and', 'carried', 'to', 'the', 'next', 'paragraph', 'when', 'you', 'press', 'the', 'Enter', 'key.', 'You', 'can', 'copy', 'paragraph', 'formats', 'from', 'paragraph', 'to', 'paragraph', 'and', 'view', 'formats', 'through', 'task', 'panes.']
>>> [word.lower() for word in s.split()].count("paragraph")
9
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
0

Here's another example of splitting the paragraph into words and then looping through the word list and incrementing a counter when the target word is found.

paragraph = '''insert paragraph here'''
 wordlist = paragraph.split(" ")
 count = 0
 for word in wordlist:
     if word == "paragraph":
         count += 1
Matt
  • 13
  • 4