0
Q:

check if palindrome python

# using slicing
def is_palindrome(s):
  return s==s[::-1]

#===========
#using reversed()

# Program to check if a string is palindrome or not
my_str = 'aIbohPhoBiA'
# make it suitable for caseless comparison
my_str = my_str.casefold()
# reverse the string
rev_str = reversed(my_str)
# check if the string is equal to its reverse
if list(my_str) == list(rev_str):
   print("The string is a palindrome.")
else:
   print("The string is not a palindrome.")
    
#=============
#solve iteratively

def check_palin(word):
    for i in xrange(len(word)/2):
        if word[i] != word[-(i+1)]:
            return False
    return True
5
def check_if_palindrome(s):
  return s == s[::-1]
3
def palindrome_check(string):
    string = list(string)
    tmp = []
    #remove any spaces
    for x in range(0, len(string)):
        if(string[x] != " "):
            tmp.append(string[x].lower())
    
            
    #now reverse the string
    array1 = []
    i = 0
    j = len(tmp)-1

    while(i < len(tmp)):
        array1.append(tmp[j])
        i += 1
        j -= 1

    #check if array1 is equal to the string
    counter = 0
    for x in range(0, len(tmp)):
        if(tmp[x] == array1[x]):
            counter += 1

    #if the counter is equal to the length of the string then the word
    #is the same
    if(counter == len(tmp)):
        return True
    
    return False
3
function isPalindrome(str) {
  str = str.toLowerCase();
  return str === str.split("").reverse().join("");
}
0

New to Communities?

Join the community