Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
I want to find a palindrome in a string.
mystring = "1234321"
Why does
mystring[3:7:-1]
not work, but
second = mystring[3:7] reversed = second[::-1]
this work?
Because you have to tell python to "start" with the end index and "end" with the start index:
>>> "0123456789"[7:3:-1] '7654'
Add a comment
When you reversing the string on the fly like that, you need to put the larger index first. You should use:
mystring[7:3:-1]
instead of:
mystring[::-1] implies the start and the end.
Using [3:7:-1] you give an "increment", start index and finish index, but you are also instantly out of your string boundaries....
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.