Recursive Palindrome Function - Python Programming Exercise

In this exercise, you will develop a Python program with a recursive function to determine if a string is symmetric (a palindrome). This exercise is perfect for practicing function definition, recursion, and string manipulation in Python. By implementing this function, you will gain hands-on experience in handling function definitions, recursion, and string manipulation in Python. This exercise not only reinforces your understanding of functions but also helps you develop efficient coding practices for managing user interactions.

 Category

Mastering Functions

 Exercise

Recursive Palindrome Function

 Objective

Develop a Python program with a recursive function to determine if a string is symmetric (a palindrome). For example, "RADAR" is a palindrome.

 Example Python Exercise

 Copy Python Code
# Recursive function to check if a string is a palindrome
def is_palindrome(s):
    # Base case: if the string has 0 or 1 character, it is a palindrome
    if len(s) <= 1:
        return True

    # Compare the first and last character
    if s[0] != s[-1]:
        return False  # If characters don't match, it's not a palindrome

    # Recursive case: check the substring without the first and last characters
    return is_palindrome(s[1:-1])

# Example strings to test
test_strings = ["RADAR", "hello", "madam", "world"]

# Check if each string is a palindrome
for test_string in test_strings:
    result = is_palindrome(test_string)
    print(f"Is '{test_string}' a palindrome? {result}")

 Output

python recursive_palindrome.py
Is 'RADAR' a palindrome? True
Is 'hello' a palindrome? False
Is 'madam' a palindrome? True
Is 'world' a palindrome? False

 Share this Python Exercise

 More Python Programming Exercises of Mastering Functions

Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.

  •  Function FindMinMax

    In this exercise, you will develop a Python program with a function called "FindRange", where the user is prompted to input a minimum value (a number) and a maximum v...

  •  Function Multiply & MultiplyRecursive

    In this exercise, you will develop a Python program with two functions, Multiply and MultiplyRecursive, to compute the product of two numbers by using addition. The f...

  •  Functions: Hello and Goodbye

    In this exercise, you will develop a Python program where the main function should look like this: SayHello() and SayGoodbye(). You must define the functions SayHello...

  •  Parameterized Function

    In this exercise, you will develop a Python program where the main function should look like this: SayHello("John") and SayGoodbye(). You must define the functions Sa...

  •  Value-Returning Function

    In this exercise, you will develop a Python program where the main function should define and call a sum function that accepts two integers as parameters and returns ...

  •  Value-Returning Function V2

    In this exercise, you will develop a Python program where the main function should look like this: def main(): print("\"Hello, how are you\" contains {0} spaces".form...