我想获取单词的中间字母,但我希望结果以字符串形式显示。我该怎么办?
def get_middle(word):
a = int(len(word))
b = int(len(word)/2)
c = int(len(word)/2 - 1)
d = int(len(word)/2 - 0,5)
if a%2==0:
print(str(word[b] + word[c]))
elif a%2==1:
print(str(word[d]))
else:
print("That's not a sring!")
答案 0 :(得分:1)
def get_middle(word):
if len(word)%2 == 1:
return word[:int((len(word)-1)/2)]+word[int((len(word)-1)/2)+1:]