I'm new to Python 3, and I tried to test if my palindrome code works and passes most of the edge cases, so I created the following 2 Python files. When I ran the python3 test_palindromes.py on the terminal, it shows that 2 tests passed. And I was wondering if that is sufficient for my palindrome function. I think I have exhausted all of the cases.

Test cases for Python palindrome:



# Hint: use string.ascii_letters (all letters in ASCII character set)
import string

def is_palindrome(text):
    """A string of characters is a palindrome if it reads the same forwards and
    backwards, ignoring punctuation, whitespace, and letter casing"""
    # implement is_palindrome_iterative and is_palindrome_recursive below, then
    # change this to call your implementation to verify it passes all tests
    assert isinstance(text, str)
    return is_palindrome_iterative(text)
    # return is_palindrome_recursive(text)

def is_palindrome_iterative(text):
    # TODO: implement the is_palindrome function iteratively here
    # once implemented, change is_palindrome to call is_palindrome_iterative
    # to verify that your iterative implementation passes all tests
    Return true if all characters in the string are alphabetic and there is at
    least one character, false otherwise. Alphabetic characters are those 
    characters defined in the Unicode character database as “Letter”, i.e.,
    those with general category property being one of “Lm”, “Lt”, , “Ll”, or “Lo”.
    Note that this is different from the “Alphabetic” property 
    defined in the Unicode Standard. (edited)"""
    # First, setting up 2 pointer. First and last pointer.

    first_pointer = 0
    last_pointer = len(text) - 1
    # iteration through when the first index is less than the last index
    while(first_pointer <= last_pointer):

    # set up different while loop condition to do comparison
    # test different condition of the palindrome cases
        # Get letters only
        while not text[first_pointer].isalpha():
            first_pointer += 1
            if first_pointer > len(text) - 1:
                return True
        while not text[last_pointer].isalpha():
            last_pointer -= 1
            if last_pointer < 0:
                return True

        # Not same, return
        if(text[first_pointer].lower() != text[last_pointer].lower()):
            return False

        first_pointer += 1
        last_pointer -= 1

    return True

def main():
    import sys
    args = sys.argv[1:]  # Ignore script file name
    if len(args) > 0:
        for arg in args:
            is_pal = is_palindrome(arg)
            result = 'PASS' if is_pal else 'FAIL'
            str_not = 'a' if is_pal else 'not a'
            print('{}: {} is {} palindrome'.format(result, repr(arg), str_not))
        print('Usage: {} string1 string2 ... stringN'.format(sys.argv[0]))
        print('  checks if each argument given is a palindrome')

if __name__ == '__main__':


import unittest

class TestPalindromes(unittest.TestCase):

    def test_is_palindrome_with_mirrored_strings(self):
        # simple palindromes that are mirrored strings
        assert is_palindrome('') is True  # base case
        assert is_palindrome('A') is True  # base case
        assert is_palindrome('BB') is True
        assert is_palindrome('LOL') is True
        assert is_palindrome('noon') is True
        assert is_palindrome('radar') is True
        assert is_palindrome('racecar') is True

    # Enable each of these test cases by deleting DISABLED_
    def DISABLED_test_is_palindrome_with_mixed_casing(self):
        # palindromes with mixed leter casing
        assert is_palindrome('Bb') is True
        assert is_palindrome('NoOn') is True
        assert is_palindrome('Radar') is True
        assert is_palindrome('RaceCar') is True

    def DISABLED_test_is_palindrome_with_whitespace(self):
        # palindromes with whitespace
        assert is_palindrome('taco cat') is True
        assert is_palindrome('race car') is True
        assert is_palindrome('race fast safe car') is True

    def DISABLED_test_is_palindrome_with_whitespace_and_mixed_casing(self):
        # palindromes with whitespace and mixed letter casing
        assert is_palindrome('Taco Cat') is True
        assert is_palindrome('Race Car') is True
        assert is_palindrome('Race Fast Safe Car') is True

    def DISABLED_test_is_palindrome_with_whitespace_and_punctuation(self):
        # palindromes with whitespace and punctuation
        assert is_palindrome('taco cat!') is True
        assert is_palindrome('race, car!!') is True
        assert is_palindrome('race fast, safe car.') is True

    def DISABLED_test_is_palindrome_with_mixed_casing_and_punctuation(self):
        # palindromes with whitespace, punctuation and mixed letter casing
        assert is_palindrome('Race fast, safe car.') is True
        assert is_palindrome('Was it a car or a cat I saw?') is True
        assert is_palindrome("Go hang a salami, I'm a lasagna hog.") is True
        assert is_palindrome('A man, a plan, a canal - Panama!') is True

    def test_is_palindrome_with_non_palindromic_strings(self):
        assert is_palindrome('AB') is False  # even length
        assert is_palindrome('ABC') is False  # odd length
        assert is_palindrome('doge') is False
        assert is_palindrome('monkey') is False
        assert is_palindrome('chicken, monkey!') is False

if __name__ == '__main__':
  • \$\begingroup\$ Are you unsure if the code works as intended? \$\endgroup\$
    – Jamal
    Oct 30, 2017 at 3:10
  • \$\begingroup\$ yes. it works as intended. I ran the unit-test and it shows all 7 cases passed. ---------------------------------------------------------------------- Ran 7 tests in 0.003s OK ....... ---------------------------------------------------------------------- Ran 7 tests in 0.001s OK ➜ python3 palindromes.py 'ABA' PASS: 'ABA' is a palindrome \$\endgroup\$
    – KJC2009
    Oct 30, 2017 at 6:15

1 Answer 1


Your tests are already nice, but they don't cover everything.

A function that strips all non-letters, lowercases the result and then only compares the initial with the final character would survive the tests, but shouldn't.

To prove the above, add the following code at the top of the test_palindromes.py file:

import re

def is_palindrome(word):
    tidied = re.sub('[^A-Za-z]', '', word)
    return tidied == '' or tidied[0] == tidied[-1]

Returning true if only any character matches instead of all is a common mistake, therefore the tests should prevent this mistake from being made.

  • \$\begingroup\$ can you give me a specific example? \$\endgroup\$
    – KJC2009
    Nov 5, 2017 at 21:56
  • 1
    \$\begingroup\$ I added an example. \$\endgroup\$ Nov 5, 2017 at 22:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.