Dada una string, escriba una función de python para verificar si es palíndromo o no. Se dice que una cuerda es palíndromo si el reverso de la cuerda es igual a cuerda. Por ejemplo, «radar» es un palíndromo, pero «radix» no es un palíndromo.
Ejemplos:
Python
# function which return reverse of a string def isPalindrome(s): return s == s[::-1] # Driver code s = "malayalam" ans = isPalindrome(s) if ans: print("Yes") else: print("No")
Python
# function to check string is # palindrome or not def isPalindrome(str): # Run loop from 0 to len/2 for i in range(0, int(len(str)/2)): if str[i] != str[len(str)-i-1]: return False return True # main function s = "malayalam" ans = isPalindrome(s) if (ans): print("Yes") else: print("No")
Python
# function to check string is # palindrome or not def isPalindrome(s): # Using predefined function to # reverse to string print(s) rev = ''.join(reversed(s)) # Checking if both string are # equal or not if (s == rev): return True return False # main function s = "malayalam" ans = isPalindrome(s) if (ans): print("Yes") else: print("No")
Python
# Python program to check # if a string is palindrome # or not x = "malayalam" w = "" for i in x: w = i + w if (x == w): print("Yes") else: print("No")
Python
# Python program to check # if a string is palindrome # or not st = 'malayalam' j = -1 flag = 0 for i in st: if i != st[j]: flag = 1 break j = j - 1 if flag == 1: print("NO") else: print("Yes")
Python3
# Recursive function to check if a # string is palindrome def isPalindrome(s): # to change it the string is similar case s = s.lower() # length of s l = len(s) # if length is less than 2 if l < 2: return True # If s[0] and s[l-1] are equal elif s[0] == s[l - 1]: # Call is palindrome form substring(1,l-1) return isPalindrome(s[1: l - 1]) else: return False # Driver Code s = "MalaYaLam" ans = isPalindrome(s) if ans: print("Yes") else: print("No")
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA