Python: Search a literals string in a string and also find the location where the pattern occurs
Python Regular Expression: Exercise-20 with Solution
Write a Python program to search a literals string in a string and also find the location within the original string where the pattern occurs.
import re pattern = 'fox' text = 'The quick brown fox jumps over the lazy dog.' match = re.search(pattern, text) s = match.start() e = match.end() print('Found "%s" in "%s" from %d to %d ' % \ (match.re.pattern, match.string, s, e))
Found "fox" in "The quick brown fox jumps over the lazy dog." from 16 to 19
Visualize Python code execution:
The following tool visualize what the computer is doing step-by-step as it executes the said program:
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?