Search code examples
pythoncharpalindrome

Check for a palindrome in python


palindrome

Here is the code i have written so far:

string=input(("Enter a string:"))
for char in string:
    print(char)
if(string==string[::-1]):
      print("The string is a palindrome")
else:
      print("Not a palindrome")

Solution

  • To produce the described output, it looks like you'd need to perform character-wise comparison. What you've attempted is a straight-forward comparison of a string with it's reverse. Try this:

    string=input(("Enter a string:"))
    rev_string = string[::-1]
    for i in range(len(string)):
        print (string[i], "--", rev_string[i])
        if string[i].lower() != rev_string[i].lower():
            print("Not a palindrome")
            exit(0)
    print("The string is a palindrome")