Re.start() & Re.end() in Python | HackerRank Solution

Hello coders, today we are going to solve Re.start() & Re.end() HackerRank Solution in Python.

Re.start() & Re.end() in Python


start() & end()

These expressions return the indices of the start and end of the substring matched by the group.


>>> import re
>>> m ='\d+','1234')
>>> m.end()
>>> m.start()


You are given a string S.
Your task is to find the indices of the start and end of string k in S.

Input Format

The first line contains the string S.
The second line contains the string k.


  • 0 < len(S) < 100
  • 0 < len(k) < len(S)

Output Format

Print the tuple in this format: (start _indexend _index).
If no match is found, print (-1, -1).

Sample Input


Sample Output

(0, 1)  
(1, 2)
(4, 5)

Solution – Re.start() & Re.end() in Python

# Enter your code here. Read input from STDIN. Print output to STDOUT
import re

string = input()
substring = input()

pattern = re.compile(substring)
match =
if not match: print('(-1, -1)')
while match:
    print('({0}, {1})'.format(match.start(), match.end() - 1))
    match =, match.start() + 1)

Disclaimer: The above Problem (Re.start() & Re.end()) is generated by Hacker Rank but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.

Leave a Comment

Your email address will not be published. Required fields are marked *