Day 059 - Project 59 : Palindrome identifier

If you have any questions, comments or issues with this project please post them here!

i stripped and lowered the input but it’s still showing " A Man A Plan A Canal Panama" is not a palindrome can someone tell me what am I doing wrong here

def palindrome(word):
  if len(word)<=0:
    return "It's a Palindrome🎊"
  if word[0] != word[-1]:
    return "booo its not a palindrome!"
  return palindrome(word[1:-1])
print("palindrome finder ")
type=input("please type a word to find if its palindrome or not \n~ ").strip().lower()
print()
print(palindrome(type))

Hi Rocko,
Looks like you need to remove the spaces when taking input. You can do this by using the .replace() function:

def palindrome(word):
	if len(word) <= 0:
		return "It's a palindrome!"
	if word[0] != word[-1]:
		return "boo, it's not a palindrome."
	return palindrome(word[1:-1])

print("palindrome finder")
word = input("please type a word to find out if it's a palindrome or not\n > ").strip().lower().replace(" ", "") # Replace any spaces with nothing
print("\n" + palindrome(word))
3 Likes

@QwertyQwerty88 ,

oO, what’s the point of the strip() doesn’t it do the same as removing spaces whats the difference between these two can you tell plz? anyways thanks for the quick reply! much appreciated :people_hugging:

1 Like

I believe .strip() only removes whitespace at the end of what you input, for example:

name = input("What's your name?\n").strip() # Taking user input and stripping it

Now if you were to enter something with trailing whitespace (ex: "Rocko ") the extra space at the end would get cut off.

Hope this helped!

4 Likes

@QwertyQwerty88,
oOh neat thnx and sorry for the late reply fell asleep and then forgot about it totally lol … anyways happy new year!

3 Likes

Happy new year! :confetti_ball: (Posts must be 20+ characters lol)

2 Likes

hahaha cheers bro enjoy :confetti_ball::confetti_ball::partying_face::partying_face:

2 Likes

I think i’ve done it wrong but it works :))

Sooo … it’s not stupid if it works :slight_smile: … until it’s not …

while True:
    word = input("Write a word: ").lower()
    wordrev = word[::-1]
    if word == wordrev:
        print(f"{word.capitalize()} is a palindrome!")
    else:
        print(f"{word.capitalize()} is not a palindrome!")

yes … you can stupid proof it … and so on … but it does what its supposed to do … corect ?

1 Like

Just FYI, if you want to save yourself some typing, .title() capitalizes the first letter of your word and .upper() makes the whole thing capital. There’s nothing wrong with .capitalize(), I just prefer shorter phrases.

5 Likes

Thank Elijah.
Laziness is the first clue that gives away a coder :)))
BTW:
How do you post snippets of code nicely on the forum. I see your functions are nicely printed too …

This works on Ask, Replit, and .md files.
Use the backtick three times to start code on a line, then type code on line(s), then three more backticks on another line. You can specify the language by typing it (no space) after the first three backticks (e.g., Python). The backtick is the same thing you use for single line code snippets. It’s to the left of the 1 on an American keyboard. IDK about other keyboard layouts.
EDIT: For indents, you have to type the spaces yourself. :slightly_frowning_face:

3 Likes

This is my best with recursion!

def recursion(value1, value2):
  if len(llista) == 1 or len(llista) == 0:
    return
  elif llista[value1] == llista[value2]:
    llista.pop(value1)
    llista.pop(value2)
    recursion(value1, value2)
  else:
    return

paraula = input("escriu una paraula: ")
paraula = paraula.lower()
if " " in paraula:
  paraula = paraula.strip()
llista = []

for i in paraula:
  i.split()
  llista.append(i)

recursion(0, -1)

if len(llista) == 1 or len(llista) == 0:
  print("es un palindrom")
else:
  print("no es un palindrom")

In the hints it was said to remove the letters once compared
So I’ve put them into a list, and then pop them!

ah boy. a minute into the solution video and I see that my working solution was not brainy enough. :face_exhaling:

while True:
  word = input("Type a word > ").strip().lower()

  if word == word[::-1]:
    print(f"{word} is a palindrome! True!")
  else:
    print(f"{word} not a palindrome. False.")```
2 Likes

What do you mean by this?

1 Like

once I saw David pull out the len() parameter in his solution (which I first thought about, but couldn’t figure out how to incorporate it), I knew I was taking a shorter route to the solution.

plus, there’s no recursion in my solution. This was a tough challenge for me.

3 Likes

Hello! So this is my code, ig it’s working right.


def palindrome(word):
  if word[0::].capitalize()==word[::-1].capitalize():
    return word

print("~Palindrome Checker~")
print()
word = input("Input a word > ")
print()
if word==palindrome(word):
  print(f"{word} is a palindrome!")
else:
  print(f"{word} is NOT a palindrome!")
2 Likes
def palindrome(word):
    # Remove non-alphanumeric characters and spaces
    cleaned_word = "".join(char for char in word if char.isalnum())
    
    if len(cleaned_word) <= 1: 
        return "It's a Palindrome🎊"
    if cleaned_word[0] != cleaned_word[-1]: 
        return "Booo it's not a palindrome!"
    return palindrome(cleaned_word[1:-1])  

print("Palindrome finder")
user_input = input("Please type a word to find if it's a palindrome or not:\n~ ").strip().lower()
print()
print(palindrome(user_input))

works as well :].

That does not. .strip() removes the spaces at the start and end of strings. Not in-between, which is what Rocko wanted. :slight_smile:

1 Like