Palindrome In Python

Hi, I have recently started learning Python. Like any novice I started by building my projects, I am stuck on Palindrome Project

Are there any rectifications I need in my code:-

userInput = input('Type a word!!!! ').lower()

emptyStr = ''

def checkIfPalindrome(word):
  strForPalindrome = emptyStr.join(list(reversed(word)))
  print(strForPalindrome)
  checkPalindrome = emptyStr.join(reversed(strForPalindrome))
  print(checkPalindrome)

  if userInput == checkPalindrome:
    print("it's a palindrome")
  else:
    print("it's not a palindrome")
checkIfPalindrome(userInput)

Please provide more detail on how you are stuck.

Is there an error?
What have you done to debug?

userInput = input('Type a word!!! ').lower()

emptyStr = ‘’

def checkIfPalindrome(word):
strForPalindrome = emptyStr.join(reversed(word))
print(strForPalindrome)

“”“Adding reversed string to list is unnecessary. Just use the emptyStr.join(reversed(word))”“”

if word == strForPalindrome:
print(“it’s a palindrome”)
else:
print(“it’s not a palindrome”)
checkIfPalindrome(userInput)

1 Like

For a start, instead of declaring an emptyStr → you can just write ''.join(stuff) plus you should never reference anything from outside the function other than constants.
Then for some order - usually you would put the setup first, meaning the definition of the function.
Within your function you reference userInput which is from outside, instead of word which you should use.

As for the check - what are you doing there? First you reverse the word, turn it into a list and turn the list back into a string… And then you reverse it again? aren’t you reversing the string twice?

The logic behind reversing the reversed string is that we get the original word. Hence I said if original word is equal to reversed reversed string then return “it’s a palindrome”

So the first line reverses the original word, but the second line of code reverses the reversed code hence we get the original code.

So you mean I shouldn’t be using list() method? Why?

The else block doesn’t seem to work. That’s why I posted here

I just don’t get one thing clear
Here:-

def checkIfPalindrome(word):
strForPalindrome = emptyStr.join(reversed(word))
print(strForPalindrome)

“”“Adding reversed string to list is unnecessary. Just use the emptyStr.join(reversed(word))”“”

if word == strForPalindrome:
print(“it’s a palindrome”)
else:
print(“it’s not a palindrome”)

The code worked perfectly as I had envisioned. But one thing I don’t get is that strForPalindrome is the reverse of the original word so putting them in the if block shouldn’t be equal. As the reversed word is the reverse of the original word.

I think you haven’t understood the meaning of palindrome.

Here is a palindrome:

anna

If we read it from left to right
a n n a
And right to left
a n n a

So a palindrome is a palindrome because if you read it in either direction, you get the same series of characters in the same order.

Your definition of palindrome is not correct ( a string which when reversed twice equals itself )

Hanna → annaH → Hanna

Everything will equal itself if you reverse it twice.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.