w3resource

Python: Find all adverbs and their positions in a given sentence


46. Find Adverbs with Position

Write a Python program to find all adverbs and their positions in a given sentence.

Sample Solution:

Python Code:

import re
text = "Clearly, he has no excuse for such behavior."
for m in re.finditer(r"\w+ly", text):
    print('%d-%d: %s' % (m.start(), m.end(), m.group(0)))
	

Sample Output:

0-7: Clearly 

Pictorial Presentation:

Python: Regular Expression - Find all adverbs and their positions in a given sentence.

Flowchart:

Flowchart: Regular Expression - Find all adverbs and their positions in a given sentence.

For more Practice: Solve these Related Problems:

  • Write a Python program to find all words ending in "ly" in a sentence and print their positions.
  • Write a Python script to search a sentence for adverbs (ending in "ly") and then output each adverb with its index.
  • Write a Python program to extract and list all adverbs from a text along with their start and end positions.
  • Write a Python program to locate adverbs in a sentence using regex and print a tuple of each adverb and its character index.

Go to:


Previous: Write a Python program to remove the ANSI escape sequences from a string.
Next: Write a Python program to split a string with multiple delimiters.

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.