• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Check if Word is Palindrome Using Recursion with Python

Check if Word is Palindrome Using Recursion with Python

May 8, 2022 Leave a Comment

You can use a recursive function in Python to easily check if a word is a palindrome.

def checkPalindrome(word):
    if len(word) < 2: 
        return True
    if word[0] != word[-1]: 
        return False
    return checkPalindrome(word[1:-1])

print(checkPalindrome("hello"))
print(checkPalindrome("anna"))

#Output:
False
True

When working in Python, recursion and recursive functions are very useful and powerful if used correctly.

One such case where recursion can be used is if we want to check if a word is a palindrome or not.

A palindrome is a word which is the same spelled forward and backward.

For recursion, we need to define a base case and a recursive step.

The base case for our recursive palindrome function is if our word has less than two letters. By definition, a word which is 0 or 1 letters is a palindrome.

The recursive step for our recursive palindrome function is to check if the first letter and last letter are equal. If they are equal, then we should remove the first and last character and check the resulting string.

In the case the first and last characters are not equal, then we should return False since the given word is not a palindrome.

Below is a function which will check if a word is a palindrome using recursion in Python.

def checkPalindrome(word):
    if len(word) < 2: 
        return True
    if word[0] != word[-1]: 
        return False
    return checkPalindrome(word[1:-1])

print(checkPalindrome("hello"))
print(checkPalindrome("anna"))

#Output:
False
True

Hopefully this article has been useful for you to learn how to make a recursive function to check if a word is a palindrome in Python.

Other Articles You'll Also Like:

  • 1.  Python max() function – Get Maximum of List or Dictionary with max() Function
  • 2.  Using Python to Add Items to Set
  • 3.  Using Python to Calculate Average of List of Numbers
  • 4.  rfind Python – Find Last Occurrence of Substring in String
  • 5.  Examples of Recursion in Python
  • 6.  Check if Number is Between Two Numbers Using Python
  • 7.  pandas percentile – Calculate Percentiles of Series or Columns in DataFrame
  • 8.  Using Python to Check if Number is Divisible by Another Number
  • 9.  Python cube root – Find Cube Root of Number With math.pow() Function
  • 10.  Deque Peek and Queue Peek Functions in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy