Quiz>
You are given a string and two markers (the initial one and final).
You have to find a substring enclosed between these two markers.
But there are a few important conditions.
This is a simplified version of the Between Markers mission.
The initial and final markers are always different.
The initial and final markers are always 1 char size.
The initial and final markers always exist in a string and go one after another.
Input:
Three arguments.
All of them are strings. The second and third arguments are the initial and final markers.
Output:
A string.
Example:
between_markers('What is >apple<', '>', '<') == 'apple'
How it is used:
For text parsing.
Precondition:
There can't be more than one final and one initial markers.
def between_markers(text: str, begin: str, end: str) -> str:
"""
returns substring between two given markers
"""
# your code here
return ''
if __name__ == '__main__':
print('Example:')
print(between_markers('What is >apple<', '>', '<'))
# These "asserts" are used for self-checking and not for testing
assert between_markers('What is >apple<', '>', '<') == "apple"
assert between_markers('What is [apple]', '[', ']') == "apple"
assert between_markers('What is ><', '>', '<') == ""
assert between_markers('>apple<', '>', '<') == "apple"
print('Wow, you are doing pretty good. Time to check it!')
Solve>
1. find를 사용하면 문자열의 위치가 반환 되므로 인덱싱이 0부터 시작하므로 시작은 +1을 해줘야지
값을 가져올 수 있다.
그렇게 해서 슬라이싱을 해서 추출한다.
def between_markers(text: str, begin: str, end: str):
return text[text.find(begin)+1:text.find(end)]
Code>
def between_markers(text: str, begin: str, end: str):
return text[text.find(begin)+1:text.find(end)]
Example>
if __name__ == '__main__':
print('Example:')
print(between_markers('What is >apple<', '>', '<'))
# These "asserts" are used for self-checking and not for testing
assert between_markers('What is >apple<', '>', '<') == "apple"
assert between_markers('What is [apple]', '[', ']') == "apple"
assert between_markers('What is ><', '>', '<') == ""
assert between_markers('>apple<', '>', '<') == "apple"
print('Wow, you are doing pretty good. Time to check it!')
Result>
Example:
apple
Wow, you are doing pretty good. Time to check it!
'Python_Matter > [Check_IO]Elementary' 카테고리의 다른 글
Elementary Map (0) | 2020.04.12 |
---|---|
Nearest Value (0) | 2020.04.11 |
Beginning Zeros (0) | 2020.04.11 |
Split Pairs (0) | 2020.04.11 |
Max Digit (0) | 2020.04.11 |