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.
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.
startindex_ 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 :
It will print the below output :
- 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 :
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.
- Python program to convert an integer number to octal
- Python program to print the odd numbers in a given range
- Python program to find the gcd of two numbers using fractions module
- Python program to print all combinations of three numbers
- Python program to find out the sum of all digits of a number
- Python program to find all numbers in a string