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

Hello coders, In this post, you will learn how to solve Re.start() & Re.end() in Python HackerRank Solution. This problem is a part of the Python Hacker Rank series.

Re.findall() & Re.finditer() in Python - HackerRank Solution
Re.start() & Re.end() in Python HackerRank Solution

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

problem

start() & end()
These expressions return the indices of the start and end of the substring matched by the group.
Code :

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

Task :

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.

Constraints :

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

Output Format :

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

Sample Input :

aaadaa
aa

Sample Output :

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

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

import re
S, k = input(), input()
matches = re.finditer(r'(?=(' + k + '))', S)
anymatch = False
for match in matches:
    anymatch = True
    print((match.start(1), match.end(1) - 1))
if anymatch == False:
    print((-1, -1))

Disclaimer: The above Problem (Re.start() & Re.end() in Python) is generated by Hackerrank but the Solution is Provided by Chase2Learn. This tutorial is only for Educational and Learning purposes. Authority if any of the queries regarding this post or website fill the following contact form thank you.

Sharing Is Caring

Leave a Comment