Python program to check palindrome using one if-else

Published by admin on

Python check palindrome using one if-else

Python Program to check if a string is a palindrome or not :

What is a palindrome String?

A String which is same in both direction is called a palindrome string. If we reverse a palindrome String, it will remain the same. e.g. “123454321” is a palindrome String but “12345 ” is not.

To check if a string is a palindrome or not, we will reverse it and compare it with the original one. If both are equal, it is a palindrome string, else not.

As you can see here, palindrome checking is really easy. The only thing we need to know is the reverse process. If we can reverse a string, we can verify it is a palindrome or not. In this python tutorial, we will learn how to reverse a string and how to check if a string is a palindrome or not.

Before moving to the program, let me quickly introduce you to the concept of slicing a python string. We know that strings are a sequence of characters and they are immutable. Slicing is used to extract a part of the string.

 

Python Slicing operation :

Using python slicing, we can extract one part of a String. Slicing is really easy to implement. Slicing instructions are placed inside a square bracket [] separating them with ‘:’.

 

The syntax of slicing is like below :

str[start_index : end_index : step]

 

str is the string we are slicing.

start_index is the index of the string where slicing should start.

end_index is the end index for the slicing.

step is the step used in the slicing.

 

If we don’t pass any value for the start_index, it will start from the start of the string. Similarly, if we don’t pass any value for the end_index, it will consider the whole string from the start_index to the end.

The index of the characters in a string starts from 0 i.e. zero for the first character, one for the second character, two for the third etc.

 

# method 1
str = "12345"

print(str[1:3:1])

print(str[2:0:-1])

print(str[::1])

print(str[::-1])



# method 2
str1 = "123454321"

if str1 == str1[::-1]:
    print("Palindrome...")
else:
    print("Not palindrome...")

 

Let’s take a look into the below example :

python string slicing

 

It will print the below output :

python string slicing

 

str[1:3:1] means it will take the string from character position 1 to 2. “1 ” means it will read from left to right. So the output is ‘23 ‘.

– Similarly “-1 “ means it will read from right to left. str[2:0:-1] will read from character position “2 ” to “1 ” in reverse direction and it prints the output “32

– If the first and the second parameter is not mentioned, it will take the full string. The third print statement is printing the same string and the fourth statement is printing the string in reverse order. The third parameter ‘-1‘ is used to print the string in reverse.

 

Now, to check for the palindrome, we need to reverse the string and check if it is the same as the original or not. The following program will do that :

python check palindrome

 

By using only one if-else condition, we can check if a String is a palindrome or not in python. We are using ‘str1[::-1]‘ to reverse it. “==” is used to compare this reverse string with the original one.

 

Categories: python

1 Comment

Anshul · June 22, 2017 at 8:40 am

def check_if_palindrome(some_string):
return True if (some_string == some_string[]::-1]) else False

Leave a Reply

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