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.