How to check if a given string is a palindrome
A string is said to be a palindrome if it can be read the same from left to right as right to left.
Examples:- “Naman”, “Madam”,“Civic”.
First method
Usws the list to check if a given string is a palindrome:
def is_it_Palindrome(s):return s == s[::-1]# Driver codes = "civic"a = is_it_Palindrome(s)if a:print("Yes")else:print("No")
Second method
Uses the for loop(iterator) to check if a given string is a palindrome:
def is_it_Palindrome(str):# Run loop from 0 to len//2for i in range(0, len(str)//2):if str[i] != str[len(str)-i-1]:return Falsereturn True# main functions = "civic"a = is_it_Palindrome(s)if (a):print("Yes")else:print("No")
Third method
Uses the built-in reversed function to check if a given string is a palindrome.
def is_it_Palindrome(s):# Using predefined function to# reverse to string print(s)rev = rev = ''.join(reversed(s))# Checking if both string are# equal or notif (s == rev):return Truereturn False# main functions = "civic"a = is_it_Palindrome(s)if (a):print("Yes")else:print("No")