Python Programming Examples Tutorial Index

Python String Programs

A palindrome is a word, phrase, number, or other sequences of characters that reads the same backward or forward. In other words, it is a sequence of characters that is the same when read from either direction.

Here are a few examples of palindromes:

  • "racecar"
  • "level"
  • "madam"
  • "rotator"
  • "deified"
  • "civic"

Palindromes are words, or phrases spelled the same way forwards and backwards. They are often used as a simple example of recursion in computer science and programming. Palindromes can be found in any language and can be made up of letters, numbers, or a combination of both.

Here are the steps to check whether a given sequence is a palindrome or not:

  1. First, a function needs to be defined that takes in a sequence as input and returns a Boolean value indicating whether the sequence is a palindrome or not.
  2. Next, we need to compare the first character of the sequence with the last character, the second character with the second-to-last character, and so on. If any pair of characters do not match, we can return False immediately since the sequence is not a palindrome.
  3. If we reach the middle of the sequence and all the pairs of characters have matched, we can return True, since the sequence is a palindrome.

Here is an example code in Python that demonstrates how to implement this algorithm:


def is_palindrome(s):
    # Check if the length of the string is 0 or 1
    if len(s) < 2:
        return True

    # Compare the first and last characters
    if s[0] != s[-1]:
        return False

    # Recursively check the rest of the string
    return is_palindrome(s[1:-1])

This function uses recursion to check the palindromes of the sequence. At each step, it compares the first and last characters and then calls itself with the sub-sequence between these two characters. If the sequence length is 0 or 1, it returns True immediately since all sequences of this length are palindromes.

To use this function, simply pass in a sequence as an argument, and it will return True if the sequence is a palindrome and False if it is not.

Here are some examples of how to use the above function:

print(is_palindrome("racecar")) # True

print(is_palindrome("hello")) # False

print(is_palindrome("a")) # True

print(is_palindrome("")) # True

Here is another example of how to check if a given string is a palindrome using Python:


def is_palindrome(string):
  # remove any spaces and make all characters lowercase
  string = string.replace(" ", "").lower()

  # reverse the string
  reversed_string = string[::-1]

  # check if the string is equal to its reverse
  return string == reversed_string

# test the function
print(is_palindrome("racecar"))  # True
print(is_palindrome("hello"))  # False

In the example above, we first remove any spaces and convert the string to lowercase so that we ignore the difference in capitalization and spacing. Then, we use slicing to reverse the string. Finally, we compare the original string with the reversed string to determine whether it is a palindrome.

You can also use this approach to check if a number is a palindrome. For example, 121 is a palindrome because it is the same when read backward or forward. To check if a number is a palindrome, you can first convert it to a string and then follow the same steps as above.

Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram