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.
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.
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.
Begin by defining a function called
is_palindrome
that takes a string as input.
def is_palindrome(string):
- 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]
- 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, returnFalse
.
def is_palindrome(string):
reversed_string = string[::-1]
if string == reversed_string:
return True
else:
return False
- 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
Create a function called
is_palindrome
that takes a string as input.Initialize two variables,
start
andend
, with values 0 and the length of the string minus 1, respectively. These variables will help us iterate through the string from both ends.Create a loop that continues until
start
is less than or equal toend
.Inside the loop, check if the characters at positions
start
andend
are equal. If they are not, returnFalse
immediately, as it means the string is not a palindrome.Increment
start
by 1 and decrementend
by 1 to move closer to the middle of the string.If the loop completes without returning
False
, it means the string is a palindrome. In that case, returnTrue
.
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
Create a function called
is_palindrome
that takes a string as input.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
.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.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!