This code will work on any string, whether it is a word, or a phrase or sentence. Technically, "never odd or even" is not a palindrome though, because the 5th character is 'r' and the 5th last character is ' ', which don't match. Unless we eliminate the spaces and we get "neverofforeven", but that's sort of a different problem: "is the string in a palindrome if all spaces are removed". And then we need to know whether we are removing things like punctuation marks too, because maybe we only want to consider letters. What you could do in that case is filter the string to get it down to only letters, for example like this: stackoverflow.com/a/12400392. Or maybe only letters and digits, like some of these examples: www.geeksforgeeks.org/python-remove-all-characters-except-letters-and-numbers/. Hopefully this helps Daniel! :-)
Can you just reverse the string using string1[::-1] then check? word = str(input(“Enter word: “)) if word == word[::-1]: print(“The word,”, word,” is a palindrome”) else: print(“The word,”, word,” is not a palindrome”)
You explained the positional comparison with such clarity - thank you. My Python tutor explained in a manner I failed to understand.
You’re welcome, I’m glad the explanation helped you out! :-)
Clear, simple and substantive explanation. Thanks!
You’re welcome Antoni, I’m glad you found the explanation clear and simple! :-)
best so far! Thanks so much for the awesome explanation🙏
Such a clear explanation.. thank you so much dear sir :)
You’re welcome! :-)
Thank you, Great Explanation. What about the time complexity ?
THANK YOU SO MUCHHH ❤😊 THIS HELPED A LOT
Thank you, was really helpful !
You’re welcome, I’m glad it was helpful for you! :-)
this helps, but what about if the input is a phrase or sentence instead? like "never odd or even"
This code will work on any string, whether it is a word, or a phrase or sentence. Technically, "never odd or even" is not a palindrome though, because the 5th character is 'r' and the 5th last character is ' ', which don't match. Unless we eliminate the spaces and we get "neverofforeven", but that's sort of a different problem: "is the string in a palindrome if all spaces are removed". And then we need to know whether we are removing things like punctuation marks too, because maybe we only want to consider letters. What you could do in that case is filter the string to get it down to only letters, for example like this: stackoverflow.com/a/12400392. Or maybe only letters and digits, like some of these examples: www.geeksforgeeks.org/python-remove-all-characters-except-letters-and-numbers/. Hopefully this helps Daniel! :-)
@@PortfolioCourses thank you!
You're welcome! :-)
Thanks from Bangladesh
Can you just reverse the string using string1[::-1] then check?
word = str(input(“Enter word: “))
if word == word[::-1]:
print(“The word,”, word,” is a palindrome”)
else:
print(“The word,”, word,” is not a palindrome”)
Can you say using functions??? In that simple manner..
Thank you
You’re welcome! :-)
Thank you a lot! It was really helpful!
You're very welcome, I'm glad to hear it was helpful! :-)