unable to get the expected answer for string palindrom

Question:

Every time i am geting else condition as true. If i pass input string as "ama" then code should give input string is palindrom. But i am getting string is not palindrom.
Input: ami
output: ami
Expected:string is palindrom

Input: amit
output: tima
Expected:string is n palindrom

def str_rev (input_str):
    print("input_str:", input_str)
    rev_str = " "
        for i in (input_str):
            rev_str = i + rev_str
    print("inp_str:", input_str)    
    print("rev_str:", rev_str)
    if (input_str == rev_str):
        print("string is palindrom")
    else:
        print("string is not palindrom")
    return  rev_str       
        
str = input ("Enter the string:")
print("org string:", str)
final_str= str_rev (str)
print("reverse string:", final_str)
Asked By: Sudha Singh

||

Answers:

A palindrome is a word that is the same backwards and forwards. Therefore ami is not a palindrome.

Answered By: Trimonu

At a quick glance, your formatting is off, but I think your problem is with white space. Change:

rev_str = " "

to

rev_str = ""

to get rid of that extra white space.

In fact, you can trim your strings before comparing, with the .strip() command to remove any leading or trailing white space.

'  hi '.strip() --> 'hi'
Answered By: GaryMBloom

you got bug at line 3
rev_str = " "
should be
rev_str = "" #empty string

otherwise, you create a new string with empty space at the start
Jarda

Answered By: Jaroslav Petr
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.