This is supposed to be a fairly simple question, but this somehow tripped me up. Some kind of symbols, eg: <<>>, [] and a word is passed as a parameter into the function 'encase_word', and I have to somehow get the word inside the symbols.
Test cases and expected output:
print(encase_word('<<>>', 'Hello')) #Expected output: <<hello>>
print(encase_word('[]', 'Hello')) #Expected output: [Hello]
Full code
def main():
print(encase_word('<<>>', 'Hello')) #Expected output: <<hello>>
print(encase_word('[]', 'Hello')) #Expected output: [Hello]
def encase_word(out, word):
get_1 = out[:2]
get_2 = out[2:]
make_word = get_1 + word + get_2
return make_word
main()
However, I'm struggling to get the word inside the square brackets where I would get []Hello instead of [Hello].
Try using division to get the length of the string divided by 2 and get the first n then add the word the add the last n:
def main():
print(encase_word('<<>>', 'Hello')) #<<hello>>
print(encase_word('[]', 'Hello')) #[Hello]
def encase_word(out, word):
return out[:len(out)//2] + word + out[len(out)//2:]
main()
Output:
<<Hello>>
[Hello]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.