r/PythonLearning • • Jun 30 '25

Check palindrome

Post image

Check whether word in reverse order is the same or not like : mom,racecar,madam

57 Upvotes

76 comments sorted by

14

u/FoolsSeldom Jun 30 '25

Nice.

How about,

def is_palindrome(text: str) -> bool:
    return text == text[::-1]


word = input('Enter a word: ')
print(f"{word} is {'not ' if not is_palindrome(word) else ''}a palindrome.")

7

u/dlo3 Jun 30 '25

Looks solid!

Also consider if the user enters multiple words, i.e. strings separated by whitespace. Edge case, but could be a good way to practice some string operations :) never expect anyone to use your software as it’s intended to be used 😂

3

u/N0-T0night Jun 30 '25

Ok accepted it wait 4 me

1

u/N0-T0night Jun 30 '25

Firstly thx 3 ur words This isn't sw it's like sharing ideas not projects

1

u/Interesting_Bee2992 Jun 30 '25

Exactly this. When ever I do a small project I find the best practice is to make that project as complete as possible. Example. Say you making hangman, you would expect the use to input strings only. But what if he typed a number ? What if he just pressed enter without typing anything? The way I've come to learn for best practice if you using if else statements is ... Use the if / Elif statements as your error handling and then if all them pass execute the else statement. Basically think of the else statement as your success statement.

If you taking input you should first check they actually typed something. You can do this like.

Var = input("type something" If not var: Print ("must enter a word ") Continue Elif another problem: Do this Else: (Run program normally)

Either this or raise an exception with expected error type.

Note.. I'm just learning myself so If I'm incorrect about this I'll be very thankful for the correction.

7

u/iMrProfessor Jun 30 '25

Code is correct. Focus on time complexity as well. Interviewer always focus on efficient solution. Try using two pointer approach.

3

u/N0-T0night Jun 30 '25

U can enroll it in comments

4

u/CptMisterNibbles Jun 30 '25

What “interviewer”? OP didn’t mention one.

Two pointer solution doesn’t have a different time complexity: it’s still a linear O(n). Also, if you are going this route there is no need for two pointers, just use a single index value and compare str[idx] to str[len(str) -idx -1]

An interviewer might want a more explicit answer, but if efficiency is your goal this is better. Try it yourself, write a loop and manually compare string elements one by one and use timeit. It will not be faster. The str class is written in c and it’s built in comparison is going to crush a manual python loop structure. 

1

u/iMrProfessor Jun 30 '25

Thanks for pointing out this solution. It is also another way to solve this problem.

Here is my solution:

def is_palindrome(s): low = 0 high = len(s) - 1

while low < high:
    if s[low] != s[high]:
        return False
    left += 1
    right -= 1

return True

1

u/[deleted] Jun 30 '25

I don't mean to be that guy, but same time complexity does not mean the same time.

Since the two pointer approach only has to check half the word per pointer in the worse case, so is likely faster and definitely is faster in the best case.

For those who want a breakdown of why:

Think about it, if the first half of the word is the same as the second half of the word, it's a palindrome, and you can do all the work in one loop, since you just do length-index for one side and index for the other. Plus you can exit whenever there's a mismatch between the two points, rather than reversing the whole word before checking.

Anyways, this is more of an educational point. In real world use-cases, its a difference of nanoseconds, but a good problem to think about!

Hope thats helpful

2

u/CptMisterNibbles Jun 30 '25 edited Jun 30 '25

I suggested using timeit for a reason; I sincerely encourage you to do so. You will find that while in theory checking character by character seems faster from a logical standpoint, in reality it will not be due to the overhead. Cpythons implementation of the str class is highly optimized, its comparator even more so. Taking a slice and using the built in comparator is going to crush a Python for loop checking index by index, and this is true whether the input is 8 chars or 100,000. Also, Cs comparator is fail fast so, contrary to other users suggestions (not your own though), it doesn’t continue to check every index once a mismatch has been found. It does check the entire string instead of half though if it is a palindrome.

That was an educational point, but a misguided one. Real life coding is not leetcode micro-optimizations. Theory is not reality. Don’t try to outwit the compiler/interpreter. While it is an excellent early exercise to learn the “clever” way to explicitly write out an algorithm like checking a palindrome, always recall the words of Donald Knuth:

“Premature optimization is the root of all evil”

(Running a million comparisons of either method, on inputs of random length between 3 and  10,000 I consistently get slicing and reversing to be at least 5-25x faster than an iterative check. Sometimes 1000x faster. I can post a plot of time vs string length later) 

0

u/[deleted] Jul 02 '25

[deleted]

1

u/CptMisterNibbles Jul 02 '25

Correct. So do what I suggested and go use timeit and try it. Or you know, read slightly further down where I explain in how this still is by far the faster method.

Slicing, reversing  and full comparison is significantly faster than a manual iteration. Optimized built in methods will generally destroy manual methods. I did run a million trials on random length palindromes to prove this

Theory isn’t reality. Try it for yourself and see. Just because it seems like it’s doing less work if you think about the steps doesn’t mean it’s faster

2

u/NoaGaming68 Jun 30 '25

Nice! Now try without using [::-1]

2

u/N0-T0night Jun 30 '25

With loop ok

1

u/NoaGaming68 Jun 30 '25

Yes

2

u/N0-T0night Jun 30 '25

Take this but take more complexity time

2

u/Interesting_Bee2992 Jun 30 '25

Question from a beginner. Sorry. That else: after the for loop. Wouldn't it run the same without that?

For In range() ect ect: Do this.

If ect ect: Do this Else: This

Wouldn't that be that same ? Why the extra else? Sorry if question is stupid.

2

u/NoaGaming68 Jun 30 '25

Yes! An else statement after a for loop runs if the loop did finisn normally, without being stopped by a break statement.

However here there is no break, so the else statement will always runs, whick makes it behave the same as just putting the code after as you said.

2

u/Legoracyro Jul 01 '25

Some good word lists

Try going through words_alpha.txt to find any that come up.

Also try finding any emordnilaps (a word that reads as another word when reversed) too.

1

u/N0-T0night Jul 01 '25

Emordnilaps like wt?

1

u/Legoracyro Jul 01 '25

Just any word that spelled backwards, is also another word.

draw - ward

edit - tide

live - evil

loop - pool

part - trap

1

u/N0-T0night Jul 01 '25

Give me hint how 2 do that😂😂

1

u/Legoracyro Jul 01 '25

Still learning myself, so there may be a more efficient way to do it, but from what I remember while doing it myself, was to loop trough each word in the list, reversing it, and then looping through another time to check if the reversed word comes up. If that makes sense.

Doing that, you'll also get all the palindromes. Try to find a way to rule out any that show up, so you only get the emordnilaps.

1

u/N0-T0night Jun 30 '25

U can check words like mom madam racecar 1991 .... etc

1

u/IMDbRefugee Jun 30 '25

They Might Be Giants would be proud: https://www.youtube.com/watch?v=I7not2z4-oo

1

u/N0-T0night Jun 30 '25

😂😂😂 not this

1

u/RepresentativeFill26 Jun 30 '25

I don’t know the complexity of reversing a string through indexing but using a single for loop wouldn’t require additional memory.

2

u/N0-T0night Jun 30 '25

But will require more time Loop is big(o) =n where n is looping times

1

u/RepresentativeFill26 Jun 30 '25

What do you think the time complexity of reversing a string through indexing is? It’s also O(n) (note the difference in notation) since you have to copy each character.

1

u/N0-T0night Jun 30 '25

Ok but 4 looping u 'll store reversed one or not

1

u/RepresentativeFill26 Jun 30 '25

No, using a for loop will be more efficient in terms of space complexity since you don’t need to store the reversed string.

1

u/N0-T0night Jun 30 '25

If u don't store it so how could u compare Explain to me plz

1

u/RepresentativeFill26 Jun 30 '25

You can check the characters at the indices starting front and back using 2 variables.

Pseudo code: (I’m on my phone)

‘’’ is_palindrome(s): for i in range(len(s) // 2): if s[i] != s[-1 - i]: return False return True ‘’’

1

u/N0-T0night Jun 30 '25

So you store two different vars🤨

1

u/RepresentativeFill26 Jun 30 '25

No, you don’t have to store 2 variables. Please take some time reading or implementing the code and check some cases.

I understand this is a sub for learning Python but some stuff you really have to check out yourself.

1

u/N0-T0night Jun 30 '25

Ok firstly i'll check first and last one b4 looping string

→ More replies (0)

2

u/fllthdcrb Jun 30 '25

In Python, using a loop to compare individual characters is much slower than making a reversed copy through slicing and then using == to compare them whole. It has nothing to do with theoretical time complexity. It's just that built-in operations for this are a lot faster than what you get with Python code, since the built-ins use highly optimized native code, while anything written in Python has to go through libpython a lot. Try it yourself with large strings, say in the tens of MB or more. The difference is very noticeable.

Now, does it always matter? No. Certainly, on scales like what OP is working with, everything is going to run instantaneously, no matter the method used. But it's something to understand if you need to work with a lot of data.

Is there a way, in Python, that has comparable speed without making a copy? Not sure.

1

u/RepresentativeFill26 Jun 30 '25

Oh I agree 100%. But OP, no offense, seems like an absolute beginner in programming. When you start learning programming I think it is more informative to think in pointers and complexity.

1

u/good-mcrn-ing Jun 30 '25

Code does what it says it does, nice. Do you always have to check every letter to say anything about the palindromeness?

1

u/N0-T0night Jun 30 '25

It could be done if first and last r not the same ok

1

u/LeaderSevere5647 Jun 30 '25
  1. Did you intend for it to be case sensitive?

  2. Better to use f strings.

  3. No need for the trailing “word” at the end, looks awkward.

  4. Try to keep quotation marks more consistent.

2

u/N0-T0night Jun 30 '25

I can put .lower() func for word and reversed is this enough I've question is f refers to format ??

1

u/N0-T0night Jun 30 '25

OMG 😲 u r right

1

u/fllthdcrb Jun 30 '25

Are you wanting to optimize this? As it is, you are comparing each character in the normal string to each character in the reversed string. To illustrate, say we have a 5-character string in w. For the reverse, we'll use indices in the same string for clarity. The code does this:

  • w[0] == w[4]

  • w[1] == w[3]

  • w[2] == w[2]

  • w[3] == w[1]

  • w[4] == w[0]

As you can see, once it passes the middle, the remaining comparisons are redundant. In fact, even the middle one is unnecessary, since that character is guaranteed to be equal to itself. Consider figuring out how to eliminate those.

1

u/N0-T0night Jun 30 '25

Oh really nice notation thx 😊

1

u/Gandualp Jun 30 '25

You can cut time in half

1

u/N0-T0night Jun 30 '25

How

1

u/Gandualp Jun 30 '25

Try to do it without [::-1]

1

u/N0-T0night Jun 30 '25

With loop or not

1

u/Gandualp Jun 30 '25

With loop ofc

1

u/N0-T0night Jun 30 '25

1

u/Gandualp Jun 30 '25

You dont have to go until the end

1

u/N0-T0night Jun 30 '25

U mean split string

1

u/Gandualp Jun 30 '25

Check half

1

u/N0-T0night Jun 30 '25

So tell me loop range

→ More replies (0)

0

u/[deleted] Jun 30 '25

[deleted]

1

u/Gandualp Jun 30 '25

Of course my man

1

u/ModernTy Jun 30 '25

Your solution is right, just to make some obscure solution:

word = input("Write a word to check: ")

for letter1, letter2 in zip(word, reversed(word)):
    if letter1 != letter2:
        print(f"{word} is not a palindrome")
        break
else:
    print(f"{word} is a palindrome")

1

u/N0-T0night Jun 30 '25

Cool one 😎

1

u/Mck985 Jul 02 '25

You are a legend (coding through a phone)

1

u/N0-T0night Jul 03 '25

🐠 that is Kind of u 😎

1

u/BoonkeyDS Jul 02 '25

The funny thing is, the way Python is optimized, it might as well be the best method

0

u/Mammoth-Intention924 Jun 30 '25

Try it recursively

1

u/N0-T0night Jun 30 '25

How ??

1

u/JeLuF Jun 30 '25

A text is a palindrome if

  • The first letter is the same as the last letter, and

  • The text between those two letters is a palindrome

1

u/N0-T0night Jun 30 '25

Nice advice