Difference between reverse string and forward string

Viewed 23217

The is_palindrome function checks if a string is a palindrome.

A palindrome is a string that can be equally read from left to right or right to left, omitting blank spaces, and ignoring capitalization.

Examples of palindromes are words like kayak and radar, and phrases like "Never Odd or Even". 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 = ""
    reverse_string = ""
    input_string =input_string.lower()
    # Traverse through each letter of the input string
    for x in input_string:
        # Add any non-blank letters to the 
        # end of one string, and to the front
        # of the other string.

        if x!=" ":
            new_string =new_string+ x
            reverse_string = x+reverse_string
    # Compare the strings
    if new_string==reverse_string:
        return True
    return False

What is the difference between new_string+x and x+reverse_string wouldnt it produce the same effect?

13 Answers

I am a beginner in Python. I will try to answer.

While looping through the string, new_string =new_string+ x will produce result like below for word python:

1st iteration: p

2nd iteration: py

3rd iteration: pyt

Meanwhile, the reverse_string = x+reverse_string will produce results like

1st iteration: p

2nd iteration: yp

3rd iteration: typ

That is the difference between those statements. When you check it through a palindrome word, you will find no difference. Select a non palindrome word to see the difference.

Take a look on a couple of example outputs, it might help you.

    new_string:- n ,  n -reverse_string
    new_string:- ne , en -reverse_string
    new_string:- nev ,ven -reverse_string
    new_string:- neve ,even -reverse_string
    new_string:- never ,reven -reverse_string
    new_string:- nevero ,oreven -reverse_string
    new_string:- neverod ,doreven -reverse_string
    new_string:- neverodd ,ddoreven -reverse_string
    new_string:- neveroddo ,oddoreven -reverse_string
    new_string:- neveroddor ,roddoreven -reverse_string
    new_string:- neveroddore ,eroddoreven -reverse_string
    new_string:- neveroddorev ,veroddoreven -reverse_string
    new_string:- neveroddoreve ,everoddoreven -reverse_string
    new_string:- neveroddoreven ,neveroddoreven -reverse_string

    True
    new_string:- a ,  a -reverse_string
    new_string:- ab , ba -reverse_string
    new_string:- abc ,cba -reverse_string

    False
    new_string:- k ,  k -reverse_string
    new_string:- ka , ak -reverse_string
    new_string:- kay ,yak -reverse_string
    new_string:- kaya ,ayak -reverse_string
    new_string:- kayak ,kayak -reverse_string

    True
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 string in input_string.lower():
    # Add any non-blank letters to the 
    # end of one string, and to the front
    # of the other string. 
    if string.replace(" ",""):
        new_string = new_string + string
        reverse_string = string + reverse_string
# Compare the strings
if reverse_string == new_string:
    return True
return False
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 string in input_string.lower():
        # Add any non-blank letters to the 
        # end of one string, and to the front
        # of the other string. 
        if string.replace(" ",""):
            new_string = string + new_string
            reverse_string = string + reverse_string
            
    # Compare the strings
    if new_string[::-1]==reverse_string:
        return True
    return False

Make sure they all either lower or upper case letters:

def is_palindrome(input_string):
   new_string = ""
   reverse_string = ""
    for letter in input_string:
    if letter != " ":
        new_string += letter 
        reverse_string = letter + reverse_string
   if new_string.lower() == reverse_string.lower():
    return True
return False

The is_palindrome function checks if an input string is a palindrome or not, that is does it read the same forward and reverse. We initialize the with two strings with blanks and transverse through the input string, we strip it off the spaces and compare it in lower case to ignore capitalization.

def is_palindrome(input_string):
    new_string = ""
    reverse_string = ""
    
    for i in input_string:
     
        if i != "":
            new_string = new_string.strip() + i
            reverse_string = i + reverse_string.strip()
    # Compare the strings
    if new_string.lower() == reverse_string.lower():
        return True
    return False
    def is_palindrome(input_string):
        new_string = ""
        reverse_string = ""
        for letter in input_string:
            if letter.lower() != "":
                new_string = "{}{}".format(new_string, letter).strip()
                reverse_string = "{}{}".format(letter, reverse_string).strip()
        if new_string.lower().strip() == reverse_string.lower().strip():
           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`

An alternative way that uses the methods taught in the course that this question is part of.

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 i in input_string.lower():
        # 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
def is_palindrome(input_string):

    new_string = ""
    reverse_string = ""

    for letter in input_string:

        if letter != " ":
            new_string += letter
            reverse_string = new_string[::-1]

    if new_string.lower() == reverse_string.lower():
        return True
    return False

"new_string+x and x+reverse_string"

x+reverse_string appends a letter to the start of the string every time

xstring
xxstring
xxxstring

but new_string+x appends to the end of the string every time

stringx
stringxx
stringxxx

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 = new_string + letter
        reverse_string = letter + reverse_string
# Compare the strings
if new_string == reverse_string:
    return True
else:
    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

CORRECTION: This works perfectly for the solution

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

This is my first time answering xD This is what I came up with:

def is_palindrome(input_string):
    reverse_string = ""
    new_string = input_string.lower().replace(" ","")
    for n in range(1, len(new_string)+1):
        reverse_string += new_string[-n]
    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"))
Related