【Kata Daily 190916】String end with?(字母结尾)

题目:

Complete the solution so that it returns true if the first argument(string) passed in ends with the 2nd argument (also a string).

Examples:

solution('abc', 'bc') # returns true
solution('abc', 'd') # returns false

解题办法:

def solution(string, ending):
    # your code here...
    if ending == '':
        return True
    else:
        a = string[::-1][:len(ending)][::-1]
        if a == ending:
            return True
        else:
            return False

最佳解题办法:

def solution(string, ending):
    return string.endswith(ending)

知识点:

1、string.endswith(str):使用endswith可以判断是否以某个字符串结尾

猜你喜欢

转载自www.cnblogs.com/bcaixl/p/11525849.html
end