Skip to main content

How to check if a string is a palindrome in Python

How to check if a string is a palindrome in Python.

Here's a step-by-step tutorial on how to check if a string is a palindrome in Python.

  1. First, let's understand what a palindrome is. A palindrome is a word, phrase, number, or sequence of characters that reads the same backward as forward. For example, "madam" and "racecar" are palindromes.

  2. To check if a string is a palindrome, we can use multiple approaches. Let's start with the simplest one - reversing the string and comparing it with the original string.

  3. Begin by defining a function called is_palindrome that takes a string as input.

def is_palindrome(string):
  1. Inside the function, create a variable reversed_string and assign the reverse of the input string to it. We can achieve this by using string slicing with a step of -1.
def is_palindrome(string):
reversed_string = string[::-1]
  1. Next, compare the reversed string with the original string using an if statement. If they are equal, it means the string is a palindrome. In that case, return True. Otherwise, return False.
def is_palindrome(string):
reversed_string = string[::-1]

if string == reversed_string:
return True
else:
return False
  1. Now, you can call the is_palindrome function and pass a string to check if it is a palindrome. For example:
print(is_palindrome("madam"))  # Output: True
print(is_palindrome("python")) # Output: False

This approach works fine, but there are more efficient ways to check for palindromes. Here are a few additional methods:

Approach 2: Using a loop

  1. Create a function called is_palindrome that takes a string as input.

  2. Initialize two variables, start and end, with values 0 and the length of the string minus 1, respectively. These variables will help us iterate through the string from both ends.

  3. Create a loop that continues until start is less than or equal to end.

  4. Inside the loop, check if the characters at positions start and end are equal. If they are not, return False immediately, as it means the string is not a palindrome.

  5. Increment start by 1 and decrement end by 1 to move closer to the middle of the string.

  6. If the loop completes without returning False, it means the string is a palindrome. In that case, return True.

def is_palindrome(string):
start = 0
end = len(string) - 1

while start <= end:
if string[start] != string[end]:
return False
start += 1
end -= 1

return True

Approach 3: Using recursion

  1. Create a function called is_palindrome that takes a string as input.

  2. Check the base case: if the length of the string is 0 or 1, it is a palindrome (since it only has one character or no characters). In that case, return True.

  3. If the first and last characters of the string are equal, recursively call the is_palindrome function with the substring excluding the first and last characters.

  4. If the first and last characters are not equal, return False.

def is_palindrome(string):
if len(string) <= 1:
return True
elif string[0] == string[-1]:
return is_palindrome(string[1:-1])
else:
return False

These are three different approaches you can use to check if a string is a palindrome in Python. Choose the one that suits your needs best!