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?

See if you can explain what these two lines of code are doing.

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”

No you don’t. The original word is contained in the word variable.

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

You already have the original word that gets passed to the function. All you need to do is compare the original word with the reversed word that you get via reversed.

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.

In the if statement, you are checking if the original word and the reversed word (strForPalindrome) are equal. If they are equal, that means it is a palindrome. If you reverse the string , compare with the original word and they are not equal, then you know it is not a palindrome.

Example 1:
original word: abc
reversed word cba

They are not equal, so abc is not a palindrome.

Example 2:
original word: eye
reversed word: eye

They are equal, so eye is a palindrome.

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