Hey there, today we are gonna check if a string is Palindrome or not. Now, before doing that we need to understand what a palindrome is...
Basically a palindrome is defined as a string that is written the same forward and backwards. For example -: abcdbca, if you read this from backwards it will the same.
So, how can we implement this algorithm in python?
Well, it's just a few lines of code in python...
def isPalindrome(string): reversed_string = "" for i in reversed(range(len(string))): reversed_string += string[i] return string == reversed_string
In the above code, we defined a function and it takes a parameter of string. In the second line, we define a variable
reversed_string and assign it to an empty string. In the third line of code, we start a for loop with a range of length of the string in the reversed order, Because we want the reversed string of the input string. In the fourth line of code, we assign the input order of string in reversed order to the
reversed_string variable. In the fifth line of code, we check if input
string is equal to the
reversed_string if it then returns True, else returns False.
Hey, it's my first article. I hope you liked this article. I will be back with more amazing content until then enjoy.