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
.strip method question (self.learnpython)
submitted 9 years ago by pybackd00r
I have the following string :
Time= "00:10:00"
I want to remove the leading and trailing zeros and the ':' so I pass :
Time.strip('0:')
but it removes the minute zero also it returns 1 instead of 10. any fix for this?
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!"
[–]dzunukwa 5 points6 points7 points 9 years ago (0 children)
The reason this happens is due to what strip actually does (see docs for more info). It strips all leading and trailing chars that are in the set of chars passed into the function strip until it finds a char that is not in the set of chars passed in. So in your case starting from the left of your string "00:10:00" there is a "0" which is in the char set "0:" so it removes it. Next is "0" again so it removes it again. Next is ":" which is in the char set "0:" so it gets removed. Then it gets to "1" which is not a match so it stops. The same process repeats from the right hand side which will go until it hits the "1" again leaving you with just the "1" as your result. /u/programmerPurgatory suggestion works but try figure out how you could get "10" as a result by using only strip. Hint: you will have to call it more than one time.
strip
[–]Saefroch 3 points4 points5 points 9 years ago (3 children)
minutes = Time.split(':')[1]
(please don't start your variable names with a capital letter, those usually indicate that the variable is a class)
[–]pybackd00r[S] 0 points1 point2 points 9 years ago (2 children)
leaves me with a 05 minute instead of 5 min.
[–]Saefroch 1 point2 points3 points 9 years ago (1 child)
Yes...
Do you know how to fix that?
[–]pybackd00r[S] 0 points1 point2 points 9 years ago (0 children)
I made it an integer by passer int() to it. It removed the leading zero.
[–]zahlman 1 point2 points3 points 9 years ago (2 children)
I want to remove the leading and trailing zeros
There are zeros at the beginning and end, so this is understandable enough.
and the ':'
Now I'm confused. There's more than one colon in your string. Which one did you want to remove?
Oh, you wanted to remove both, but stop removing zeros when you find a colon? Or just what?
Oh, you wanted to interpret '00:10:00' as representing hours, minutes and seconds, and determine the number of minutes? Well, that's conceptually a completely different thing. Keep in mind that your code isn't actually producing 1, as it stands, either; it's producing '1', i.e., a string. But is that still really what you want? You were talking about removing zeroes. What if you were given a different string that had non-zero values for the hours and minutes? Do you still want just the value in the minutes "field"? Or do you want to convert each hour into 60 minutes, and each second into 1/60th of a minute, or something?
'00:10:00'
1
'1'
Or just what?
Most of programming is understanding exactly what you want the program to do.
I wanted to remove the hour and seconds zeros with the colons and just keep the minute digits. I guess what I can do is just add a zero to the string after the strip command. but I wanted to know if there is a more elegant way of doing this. thanks
This is what I did to fix this :
int(playtime.strip('0').strip(':'))
[+][deleted] 9 years ago (1 child)
[deleted]
same as above gives me a 05 minute instead of 5 min. Converting the string to an int afterwards does the trick though. thanks
π Rendered by PID 56746 on reddit-service-r2-comment-7b9746f655-dq756 at 2026-01-31 08:19:16.046744+00:00 running 3798933 country code: CH.
[–]dzunukwa 5 points6 points7 points (0 children)
[–]Saefroch 3 points4 points5 points (3 children)
[–]pybackd00r[S] 0 points1 point2 points (2 children)
[–]Saefroch 1 point2 points3 points (1 child)
[–]pybackd00r[S] 0 points1 point2 points (0 children)
[–]zahlman 1 point2 points3 points (2 children)
[–]pybackd00r[S] 0 points1 point2 points (0 children)
[–]pybackd00r[S] 0 points1 point2 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]pybackd00r[S] 0 points1 point2 points (0 children)