'Creating a palindrome checker with Python's loop and if statements

I am trying to follow the instructions to create a palindrome. I am given half a function, and I have to fill in the blanks. I am currently unable to get the loop to work correctly. I am also unsure how to add characters to the beginning or the end of string without using the + or a comma. I do not think that is what I am being asked to do. Here are the instructions;

The is_palindrome function checks if a string is a palindrome... Fill in the blanks in this function to return True if the passed string is a palindrome, False if not.

def is_palindrome(input_string):
    # We'll create two strings, to compare them
    new_string = input_string.replace(" ", "")
    reverse_string = input_string.replace(" ", "")
    # Traverse through each letter of the input string
    for word in input_string: # Originally, I was only given the a FOR statement here, I wrote in the rest
        new_string+=word.replace(" ","").upper()
        # Add any non-blank letters to the 
        # end of one string, and to the front
        # of the other string. 

    if ___:
        new_string = ___
        reverse_string = ___
    # # Compare the strings
      if ___:
          return True
          return False

print(is_palindrome("Never Odd or Even")) # Should be True
print(is_palindrome("abc")) # Should be False
print(is_palindrome("kayak")) # Should be True

I have removed the empty spaces and made everything the same case. I have assigned the characters to new_string, but it looks like I am supposed to use join to add the characters, but when I do the print statement does not print anything. I am unsure how to add the items in reverse order. I am not even sure if I am on the correct track, because I am unsure what the IF statement is asking. I would think I should be able to use a loop to create the string and then compare the two strings.

Also, could someone please explain why new_string.join(word) does not print anything out? How am I using it incorrectly?

Thank you very much for any possible assistance.



Solution 1:[1]

It worked for me, I have tried and change little bit from above my code it work now with below code.

You can see it pass in their test and code verification, refer below screenshots and code.

enter image description here

def is_palindrome(input_string):
    # We'll create two strings, to compare them
    new_string = ""
    reverse_string = ""
    # Traverse through each letter of the input string
    for letter in input_string.strip():
        # Add any non-blank letters to the 
        # end of one string, and to the front
        # of the other string. 
        new_string = new_string+letter.replace(" ","")
        reverse_string = letter.replace(" ","")+reverse_string
    # Compare the strings
    if new_string.lower() == reverse_string.lower():
        return True
    return False

print(is_palindrome("Never Odd or Even")) # Should be True
print(is_palindrome("abc")) # Should be False
print(is_palindrome("kayak")) # Should be True

Solution 2:[2]

def is_palindrome(input_string):
 new_string = input_string.replace(" ", "").lower()
 reverse_string = input_string.replace(" ", "").lower()[::-1]
if new_string == reverse_string:
        return True
    return False

> This should do it.

Solution 3:[3]

I have written this program,
It returns expected result as well, but it is getting rejected.
Don`t know what exactly they test internally to validate code and its correctness.
My implementation :

def is_palindrome(input_string):
    # We'll create two strings, to compare them
    new_string = ""
    reverse_string = ""
    # Traverse through each letter of the input string
    for letter in input_string.strip():
        # Add any non-blank letters to the 
        # end of one string, and to the front
        # of the other string. 
        if letter !=' ':
            new_string = new_string+letter
            reverse_string = letter+reverse_string
    # Compare the strings
    if new_string.lower() == reverse_string.lower():
        return True
    return False

print(is_palindrome("Never Odd or Even")) # Should be True
print(is_palindrome("abc")) # Should be False
print(is_palindrome("kayak")) # Should be True

enter image description here

If any one knows what exactly i am missing here ?

Solution 4:[4]

It worked for me, This code what the system expected to see.

def is_palindrome(input_string):
    # We'll create two strings, to compare them
    new_string = ""
    reverse_string = ""
    # Traverse through each letter of the input string
    for word in input_string:
        # Add any non-blank letters to the 
        # end of one string, and to the front
        # of the other string. 
        word = word.lower()
        if word != " ":
            new_string = new_string + word
            reverse_string = word + reverse_string
    # Compare the strings
    if new_string == reverse_string:
        return True
    return False

print(is_palindrome("Never Odd or Even")) # Should be True
print(is_palindrome("abc")) # Should be False
print(is_palindrome("kayak")) # Should be True

is_palindrome function

Solution 5:[5]

def is_palindrome(input_string):
    # We'll create two strings, to compare them
    new_string = ""
    reverse_string = ""
    # Traverse through each letter of the input string
    for char in input_string:
        # Add any non-blank letters to the 
        # end of one string, and to the front
        # of the other string.
        if char !=" ":
            new_string +=char.lower() 

            reverse_string =char.lower()+reverse_string

    # Compare the strings
    if new_string==reverse_string:
        return True
    return False

print(is_palindrome("Never Odd or Even")) # Should be True
print(is_palindrome("abc")) # Should be False
print(is_palindrome("kayak")) # Should be True

Solution 6:[6]

Its pretty simple one . The main approach is

  1. Convert the given string into lower-case
  2. Check for white space ""
  3. Insert into new strings

        new_string = new_string+i
        reverse_string = i+reverse_string
    
  4. Check if new-string == reverse_string

CODE :

# We'll create two strings, to compare them

input_string = input_string.lower()
new_string = ""
reverse_string = ""
# Traverse through each letter of the input string
for i in input_string:
    # Add any non-blank letters to the 
    # end of one string, and to the front
    # of the other string. 
    if i !=" ":
        new_string = new_string+i
        reverse_string = i+reverse_string
# Compare the strings
if new_string == reverse_string:
    return True
return False

print(is_palindrome("Never Odd or Even")) # Should be True
print(is_palindrome("abc")) # Should be False
print(is_palindrome("kayak")) # Should be True

OUTPUT

 True 
 False
 True

Solution 7:[7]

This works for me:

def is_palindrome(input_string):
    # We'll create two strings, to compare them
    new_string = input_string.replace(" ", "")
    new_string = new_string.lower()
    reverse_string = new_string[::-1]
    reverse_string = reverse_string.lower()
    # Traverse through each letter of the input string
    if new_string == reverse_string:
        return True
    return False


print(is_palindrome("Never Odd or Even"))  # Should be True
print(is_palindrome("abc"))  # Should be False
print(is_palindrome("kayak"))  # Should be True

Solution 8:[8]

def is_palindrome(input_string):

    # We'll create two strings, to compare them
    new_string = ""
    reverse_string = ""
    # Traverse througenter code hereh each letter of the input string
    for letter in input_string.strip().lower():
        # Add any non-blank letters to the 
        # end of one string, and to the front
        # of the other string. 
        if letter!=" ":
            new_string = new_string+letter
            reverse_string = letter+reverse_string
    # Compare the strings
    if new_string==reverse_string:
        return True
    return False

Then:

print(is_palindrome("Never Odd or Even")) # Should be True

print(is_palindrome("abc")) # Should be False

print(is_palindrome("kayak")) # Should be True

Solution 9:[9]

def is_palindrome(input_string):
    new_string = ""
    reverse_string = ""
    
    for word in input_string:
        if word != " ":
            new_string = new_string.strip().lower() + word
            reverse_string = word + reverse_string.strip().lower()
    # Compare the strings
    if new_string == reverse_string:
        return True
    return False

print(is_palindrome("Never Odd or Even")) # Should be True
print(is_palindrome("abc")) # Should be False
print(is_palindrome("kayak")) # Should be True

Solution 10:[10]

The strip() or replace() functions aren't necessarily useful in this case.

The strip() method won't remove spaces between each word in a phrase i.e. the first case 'Never Odd or Even'.

Hence, a simple if statement will do the job.

Don't forget to convert your strings to upper() or lower() cases.

def is_palindrome(input_string):
    # We'll create two strings, to compare them
    new_string = ""
    reverse_string = ""
    # Traverse through each letter of the input string
    for letter in input_string.lower():
        # Add any non-blank letters to the 
        # end of one string, and to the front
        # of the other string. 
        if letter!=" ":
            new_string += letter
            reverse_string = letter + reverse_string
    # Compare the strings
    if new_string == reverse_string:
        return True
    return False
print(is_palindrome("Never Odd or Even")) # Should be True
print(is_palindrome("abc")) # Should be False
print(is_palindrome("kayak")) # Should be True

Solution 11:[11]

I have some spare time so I try to manually indexing and it works for me :D

def is_palindrome(input_string):
    input_string = input_string.lower().replace(" ", "") '''make the string have 
                                    lower letter remove the withspace'''
    new_string = ""
    reverse_string = ""
    for n in range(len(input_string)):
        new_string += input_string[n]
        reverse_string += input_string[(len(input_string)-(1+n))] '''manually make string 
                                                from behind with indexing.'''
    if new_string == reverse_string:
        return True
    return False

Solution 12:[12]

def is_palindrome(input_string):
    new_string = input_string.lower()
    no_space = new_string.replace(" ","")

    reverse_string =new_string.replace(" ","")[::-1]
    if reverse_string==no_space:
        return True
    return False

print(is_palindrome("Never Odd or Even")) # Should be True
print(is_palindrome("abc")) # Should be False
print(is_palindrome("kayak")) # Should be True