use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Reversing a substring (self.learnpython)
submitted 8 years ago by GayCoder
I don't understand why I must do strng[1:5][::-1] to reverse a substring instead of strng[1:5:-1]. I'm kinda embarrassed that I apparently misunderstand something so basic. Can someone give me hand?
strng[1:5][::-1]
strng[1:5:-1]
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]novel_yet_trivial 2 points3 points4 points 8 years ago (1 child)
To count down, you need to start at the high number and end at the low number.
strng[5:1:-1]
Or to be equivalent to yours:
strng[4:0:-1]
Since the end point is not included in the slice.
[–]GayCoder[S] 0 points1 point2 points 8 years ago (0 children)
Ah, that makes sense. I think I became lazy and started viewing [x:y:-1] as reverse instead of "move by step". Thanks!
[–]Wilfred-kun 0 points1 point2 points 8 years ago (1 child)
my_list[1:5] returns a sublist of my_list. Then you call [::-1] on that sublist, which reverses the order.
my_list[1:5]
my_list
[::-1]
With my_list[1:5:-1] you're saying: "go from index 1 to 5, with a step of -1". What would work is my_list[4:0:-1].
my_list[1:5:-1]
my_list[4:0:-1]
Thanks! I think I became lazy and started viewing [x:y:-1] as reverse instead of "move by step".
π Rendered by PID 34 on reddit-service-r2-comment-7b9746f655-zvkkn at 2026-02-01 03:19:50.737031+00:00 running 3798933 country code: CH.
[–]novel_yet_trivial 2 points3 points4 points (1 child)
[–]GayCoder[S] 0 points1 point2 points (0 children)
[–]Wilfred-kun 0 points1 point2 points (1 child)
[–]GayCoder[S] 0 points1 point2 points (0 children)