我有一个变量作为元组,如何将其作为字符串返回

时间:2019-08-26 10:54:29

标签: python

我想获取单词的中间字母,但我希望结果以字符串形式显示。我该怎么办?

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!")

1 个答案:

答案 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:]