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

@QwertyQwerty54 ,

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!

3 Likes

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

2 Likes

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

1 Like

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

1 Like

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.

3 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:

2 Likes