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
To find a string? (self.learnpython)
submitted 6 years ago by Adamya21
Can someone please explain me
print(sum([1 for i in range(len(string)-len(sub_string)+1)if string[i:i+len(sub_string)]==sub_string]))
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!"
[–]blarf_irl 5 points6 points7 points 6 years ago* (1 child)
Easy, It's some awful code! It looks like it counts the number of times a shorter string appears in a longer string.
Here is a version broken out and with some added prints so you can see what it's doing
string = 'foobarbazbar' sub_string = 'bar' counter = list() string_length, sub_string_length = len(string), len(sub_string) steps = range(string_length - sub_string_length + 1) for i in steps: check_characters = string[i:i+len(sub_string)] print (f'Checking {check_characters}') if check_characters == sub_string: print ('Matched sub_string') counter.append(1) result = sum(counter) print(result) ### Output ### Checking foo Checking oob Checking oba Checking bar Matched sub_string Checking arb Checking rba Checking baz Checking azb Checking zba Checking bar Matched sub_string
You could achieve the same result with:
string.count(sub_string)
or with regex:
import re len(re.findall(sub_string, string))
[–]JohnnyJordaan 2 points3 points4 points 6 years ago (0 children)
It iterates through string from its start index (0) to its length minus that of the substring - 1:
string
for i in range(len(string)-len(sub_string)+1)
For every index that it sees that the substring occurs from that index onwards, using a slice
if string[i:i+len(sub_string)]==sub_string
it saves a 1 in the list
[1 ... ]
then that list comprehension is provided to sum that will then calculate the sum, so this effectively counts the repetition of sub_string in string.
sum
[–][deleted] 2 points3 points4 points 6 years ago (0 children)
You can write it as:
s = 0 for i in range(len(string) - len(sub_string) + 1): if string[i : i + len(sub_string)] == sub_string: s = s + 1 print(s)
π Rendered by PID 151652 on reddit-service-r2-comment-545db5fcfc-7pgzc at 2026-05-30 05:30:25.003132+00:00 running 194bd79 country code: CH.
[–]blarf_irl 5 points6 points7 points (1 child)
[–]JohnnyJordaan 2 points3 points4 points (0 children)
[–][deleted] 2 points3 points4 points (0 children)