0

The code isn't working

So, I have a code that must return text between two characters: cont = ("0. ZEROZEROZERO: Isome textt> 1.ONENEONE: some other text, 2. TWOTWOTWOwtqwhw: 3.THREE: qweqweqeqwe 4.FOURFOUR: qrqrqrqrqr, 5.FIVEFIVE:") def find_between(text, first, last): try: start = text.index(str(first)) end = text.index(last) print(text[start:end]) except ValueError: return "" find_between(cont, 3, ":") The code must print text between 3 and ":" But it's not printing anything, but the code runs correctly if I search between zero and ":"

17th Mar 2019, 1:56 PM
Š–Š°Š½ŠøŠ±ŠµŠŗ Š Š°Š¹ŠŗуŠ»Š¾Š²
Š–Š°Š½ŠøŠ±ŠµŠŗ Š Š°Š¹ŠŗуŠ»Š¾Š² - avatar
3 Answers
+ 3
your first case works as start and end index is fine..... for rest all cases , always search result for : is lesser than your start value... just do below change in code and it should be fine : end = text.find(last,start) above should always find last character after first character index
17th Mar 2019, 2:11 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 3
start = text.index(first + '.') Don't know if this is the only issue
17th Mar 2019, 2:07 PM
Anna
Anna - avatar
+ 1
Thank you so much!
17th Mar 2019, 2:14 PM
Š–Š°Š½ŠøŠ±ŠµŠŗ Š Š°Š¹ŠŗуŠ»Š¾Š²
Š–Š°Š½ŠøŠ±ŠµŠŗ Š Š°Š¹ŠŗуŠ»Š¾Š² - avatar