无法返回空字符串

时间:2015-05-07 15:19:09

标签: python

"我无法根据此问题返回空字符串,问题的第一部分效果很好,但关于"空字符串"问题我无法返回"

Given a string s, return a string made of the first 2
# and the last 2 chars of the original string,
# so 'spring' yields 'spng'. However, if the string length
# is less than 2, return instead the empty string.
     def both_ends(s):
# +++your code here+++
         if s <=2:
               return s[:0]
         else :
               return s[0:2] + s[-2:]

1 个答案:

答案 0 :(得分:0)

您需要检查输入的len(),然后检查return ''

def both_ends(s):
    if len(s) < 2:
        return ''
    else:
        return s[:2] + s[-2:]