Determining if string is palindrome

前端 未结 4 802
Happy的楠姐
Happy的楠姐 2020-12-20 18:43

I wrote two simple functions to determine if a string is a palindrome. I thought they were equivalent, but 2 doesn\'t work. Why is this?

1



        
4条回答
  •  温柔的废话
    2020-12-20 19:35

    For strings:

    def is_palindrome(s):
        """Return True if a string is a palindrome."""
        return s == s[::-1]
    

    For general iterables (including strings):

    def is_palindrome(iterable):
        """Return True if an iterable is a palindrome."""
        if list(iteable) == list(reversed(iterable))
    

提交回复
热议问题