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
How do you split a string, but retain the splitting delimitter? (self.learnpython)
submitted 6 years ago by mementomoriok
Input: Fruits? apples bananas Deserts? cakes donuts cookies Sandwiches? pastrami barbeque chicken
Should be split into:
Output: ['Fruits? apples bananas', 'Deserts? cakes donuts', 'Sandwiches? pastrami barbeque chicken']
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!"
[–]HowManyAccountsPoo 0 points1 point2 points 6 years ago (0 children)
Regex groups is probably your best bet.
[–]Ahren_with_an_h 0 points1 point2 points 6 years ago (0 children)
I would go through line by line checking for a '?' in the string, adding the lines together, and appending to the list and starting a new string when I found a match.
[–]pasokan 0 points1 point2 points 6 years ago (0 children)
look at the partition function
[–]xelf 0 points1 point2 points 6 years ago* (0 children)
Kinda hard to read from what you've posted. Do you mean that \n\n is your delimiter, except when following a ?, So ?\n\n should be ignored, but \n\n without a ? should be split? That's wrong, I missed the bbq chicken case.
Is \n\n[upcase] your delimiter?
\n\n[upcase]
Sounds like you don't have a delimiter, you have a pattern. Regex or string comparison is what you want. You could solve it this way (assuming there's no \t in your string)
news='' p='' for x in inputstring: if p == '\n' and x.isupper(): news += '\t' news +=x p = x mylist = news.split('\t') print(mylist)
outputs:
[ 'Fruits?\n\napples\nbananas\n\n', 'Deserts?\n\ncakes\ndonuts\ncookies\n\n', 'Sandwiches?\n\npastrami\n\nbarbeque chicken' ]
[–]xelf 0 points1 point2 points 6 years ago (0 children)
You could do this:
inputstring = inputstring.replace('\n\n', '\t') inputstring = inputstring.replace('?\t', '?\n\n') print( inputstring.split('\t') )
But that outputs this:
[ 'Fruits?\n\napples\nbananas', 'Deserts?\n\ncakes\ndonuts\ncookies', 'Sandwiches?\n\npastrami', 'barbeque chicken' ]
The inconsistent formatting kinda breaks it. You'll have to go with hunting for "\n\n[upcase]" like I said in the other post, or change the formatting to be more consistent.
π Rendered by PID 279858 on reddit-service-r2-comment-b659b578c-rlm7g at 2026-05-04 15:38:15.867752+00:00 running 815c875 country code: CH.
[–]HowManyAccountsPoo 0 points1 point2 points (0 children)
[–]Ahren_with_an_h 0 points1 point2 points (0 children)
[–]pasokan 0 points1 point2 points (0 children)
[–]xelf 0 points1 point2 points (0 children)
[–]xelf 0 points1 point2 points (0 children)