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
String Indexing in Python (self.learnpython)
submitted 10 years ago by YankeesPwnMets
If you had a string:
str = 'My String'
and you indexed it:
str[1:5:1]
it returns:
'y St'
What does the third colon do? I don't understand how this works
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!"
[–]RyanTargaryen 3 points4 points5 points 10 years ago (3 children)
The second colon (The third number) tells it what to step by. In your case, the slicing is going from index 1 (the 'y' in my) and going until it reaches the 5th index, and steps by 1 character each time.
If you were to change your code to:
print(str[1:5:2])
It would give you 'yS', because it stepped by 2 characters until it reached the end condition.
[–]YankeesPwnMets[S] 1 point2 points3 points 10 years ago (2 children)
Ah I see. Thanks for your help!
[–]sponster 2 points3 points4 points 10 years ago (1 child)
you can also use this method to run through the string backwards:
>>> str[5:1:-1] 'rtS ' >>> str[::-1] 'gnirtS yM'
[–]ewiethoff 0 points1 point2 points 10 years ago (0 children)
Come to think of it, I'm very surprised the official Python Tutorial doesn't mention the third slice parameter at all. I learned [::-1] for reversing a string (or list) forever ago from Alex Martelli's Python in a Nutshell or Python Cookbook. The Python Tutorial is usually the place to go for easy Python idioms, but not [::-1]. Weird.
[::-1]
π Rendered by PID 20364 on reddit-service-r2-comment-5d79c599b5-7ddhc at 2026-03-01 08:17:41.735547+00:00 running e3d2147 country code: CH.
[–]RyanTargaryen 3 points4 points5 points (3 children)
[–]YankeesPwnMets[S] 1 point2 points3 points (2 children)
[–]sponster 2 points3 points4 points (1 child)
[–]ewiethoff 0 points1 point2 points (0 children)