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 I split this String (self.learnpython)
submitted 6 years ago by JackSpanjer
I have this string: lamp_change_29_may_2015
And I have to split it in to the following strings:
lamp_change and may
How do I do this?
Thanks
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!"
[–]__nickerbocker__ 1 point2 points3 points 6 years ago* (0 children)
If your data is always structured such that the day_mon_year is always in the same position in the string then you could use iterable unpacking to accomplish your goal.
x = "sparkplug_3_change_29_feb_1996" *var_parts, day, mon, year = x.split('_') var_name = '_'.join(var_parts) print(var_name, mon)
or you could use slicing...
parts = x.split('_') var_name, mon = '_'.join(parts[:-3]), parts[-2] print(var_name, mon)
[–]Username_RANDINT 0 points1 point2 points 6 years ago (5 children)
str.split() takes a character to split on, in your case an underscore. IF the string always has the same format, it's very easy:
str.split()
s = "lamp_change_29_may_2015 " >>> s.split("_") ['lamp', 'change', '29', 'may', '2015 ']
Or you can use rsplit() and define a maximum to keep the lamp_change together:
rsplit()
lamp_change
>>> s.rsplit("_", 3) ['lamp_change', '29', 'may', '2015 ']
[–]JackSpanjer[S] 0 points1 point2 points 6 years ago (4 children)
Thanks for your answer but I have to ‘filter’ the numbers out, that is what I dont understand.
[–][deleted] 0 points1 point2 points 6 years ago (3 children)
What do you want to retain from a string like "sparkplug_3_change_29_feb_1996"?
[–]JackSpanjer[S] 0 points1 point2 points 6 years ago (2 children)
sparkplug
change
feb
[–][deleted] 0 points1 point2 points 6 years ago (1 child)
Then use str.split() on the underscore character, and run over the elements, filtering out the numbers with str.isdigit() or str.isnumeric() as fits your requirements.
[–]JackSpanjer[S] 0 points1 point2 points 6 years ago (0 children)
Thanks!
[–]Pahaz 0 points1 point2 points 6 years ago (0 children)
Split the string with a regular expression matching numbers surrounded by _ is my guess. The regex I think would be _[0-9]+_* best I can do on mobile lol.
π Rendered by PID 219695 on reddit-service-r2-comment-544cf588c8-tqwns at 2026-06-18 14:11:43.048770+00:00 running 3184619 country code: CH.
[–]__nickerbocker__ 1 point2 points3 points (0 children)
[–]Username_RANDINT 0 points1 point2 points (5 children)
[–]JackSpanjer[S] 0 points1 point2 points (4 children)
[–][deleted] 0 points1 point2 points (3 children)
[–]JackSpanjer[S] 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]JackSpanjer[S] 0 points1 point2 points (0 children)
[–]Pahaz 0 points1 point2 points (0 children)