How to Check if String is a Palindrome in Python

The challenge

Write a function that checks if a given string (case insensitive) is a palindrome.

The solution in Python

Option 1:

def is_palindrome(s): s = s.lower() for i, item in enumerate(s): if i<len(s)/2: if s[i]!=s[len(s)-i-1]: return False return True
Code language: Python (python)

Option 2:

def is_palindrome(s): s = s.lower() return s == s[::-1]
Code language: Python (python)

Option 3:

def is_palindrome(s): return s.lower()==s[::-1].lower()
Code language: Python (python)

Test cases to validate our solution

@test.describe('sample tests') def sample_tests(): test.assert_equals(is_palindrome('a'), True) test.assert_equals(is_palindrome('aba'), True) test.assert_equals(is_palindrome('Abba'), True) test.assert_equals(is_palindrome('malam'), True) test.assert_equals(is_palindrome('walter'), False) test.assert_equals(is_palindrome('kodok'), True) test.assert_equals(is_palindrome('Kasue'), False)
Code language: Python (python)
Tags:
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments