这应该是一个相当简单的问题,但不知何故,这让我被绊倒了。一些符号,例如:<<>>,[]和一个单词被作为参数传递到函数'encase_ word‘中,我必须以某种方式获取符号中的单词。
测试用例和预期输出:
print(encase_word('<<>>', 'Hello')) #Expected output: <<hello>>
print(encase_word('[]', 'Hello')) #Expected output: [Hello]完整代码
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()然而,我正在努力将单词放在方括号中,在那里我会得到[]Hello而不是Hello。
发布于 2019-08-29 15:09:52
尝试使用除法来获得字符串的长度除以2,并获得前n,然后添加单词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()输出:
<<Hello>>
[Hello]https://stackoverflow.com/questions/57704811
复制相似问题