views:

3778

answers:

4

Lets Say we have Zaptoit:685158:[email protected]

How do you split so it only be left 685158:[email protected]

+3  A: 

Another solution:

s = 'Zaptoit:685158:[email protected]'
s.split(':', 1)[1]
Federico Ramponi
+7  A: 
>>> s = 'Zaptoit:685158:[email protected]'
>>> s.split( ':', 1 )[1]
'685158:[email protected]'
Graeme Perrow
Note that it's not really good practice to use the variable name "str" since str() is a builtin.
Jay
Thanks Jay - I've updated the code.
Graeme Perrow
A: 
s = re.sub('^.*?:', '', s)
PEZ
^[^:]*: would be better
ʞɔıu
@PEZ: better being stopping the match on the first ':' instead of the last one
orip
@orip: I think you are mistaken -- the question mark makes it a non-greedy match that will stop at the first colon, as intended
scrible
Yeah, I think Nick means something else.
PEZ
It doesn't matter in this case, but in general, if you have choice between a negated character class and a reluctant quantifier (eg, '.*?'), the char class tends to be faster, less memory-intensive, and most importantly, more predictable.
Alan Moore
To me it's about clarity. Clearer is better. In this case .*?: is perfectly predictable and very clear. "Everything up to the first colon" is clearer than "Everything that's not a colon up to the first colon".
PEZ
+3  A: 

Another method, without using split:

s = 'Zaptoit:685158:[email protected]'
s[s.find(':')+1:]

Ex:

>>> s = 'Zaptoit:685158:[email protected]'
>>> s[s.find(':')+1:]
'685158:[email protected]'
Jay
+1. I needed to be reminded of this.
PEZ