Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Between Markers by delahere
def between_markers(text: str, begin: str, end: str) -> str:
"""
returns substring between two given markers
"""
# your code here
#
# Establish the starting index of the slice, whether the begin marker exists or not
if begin not in text:
slice_start = 0
else:
slice_start = text.find(begin) + len(begin)
#
# Establish the slice ending index, whether the end marker exists or not.
#
if end not in text:
slice_end = len(text)
else:
slice_end = text.find(end)
#
# If the slice starting and ending index are invalid (start must come first), the
# slice is an empty string.
#
if slice_end <= slice_start:
slice = ""
else:
slice = text[slice_start:slice_end]
return slice
print("Example:")
print(between_markers("What is >apple<", ">", "<"))
assert between_markers("What is >apple<", ">", "<") == "apple"
assert (
between_markers("My new site", "", "")
== "My new site"
)
assert between_markers("No[/b] hi", "[b]", "[/b]") == "No"
assert between_markers("No [b]hi", "[b]", "[/b]") == "hi"
assert between_markers("No hi", "[b]", "[/b]") == "No hi"
assert between_markers("No ", ">", "<") == ""
print("The mission is done! Click 'Check Solution' to earn rewards!")
Aug. 10, 2023