This is a Python Program to check whether a string is a palindrome or not using recursion.
The program takes a string and checks whether a string is a palindrome or not using recursion.
1. Take a string from the user.
2. Pass the string as an argument to a recursive function.
3. In the function, if the length of the string is less than 1, return True.
4. If the last letter is equal to the first letter, recursively call the function with the argument as the sliced list with the first character and last character removed else return False.
5. Use an if statement if check if the returned value is True of False and print the final result.
6. Exit.
Here is source code of the Python Program to check whether a string is a palindrome or not using recursion. The program output is also shown below.
def is_palindrome(s): if len(s) < 1: return True else: if s[0] == s[-1]: return is_palindrome(s[1:-1]) else: return False a=str(input("Enter string:")) if(is_palindrome(a)==True): print("String is a palindrome!") else: print("String isn't a palindrome!")
1. User must enter a string.
2. The string is passed as an argument to a recursive function.
3. In the function, if the length of the string is less than 1, True is returned.
4. If the last letter is equal to the first letter, the function is called recursively with the argument as the sliced list with the first character and last character removed, else return False.
5. The if statement is used to check if the returned value is True or False and the final result is printed.
Case 1: Enter string:mom String is a palindrome! Case 2: Enter string:hello String isn't a palindrome!
Sanfoundry Global Education & Learning Series – Python Programs.
To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.
- Check Python Books
- Apply for Python Internship
- Check Information Technology Books
- Practice Programming MCQs
- Apply for Programming Internship