13
votes

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

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

7

7 Answers

13
votes
>>> s = 'Zaptoit:685158:[email protected]'
>>> s.split( ':', 1 )[1]
'685158:[email protected]'
5
votes

Another solution:

s = 'Zaptoit:685158:[email protected]'
s.split(':', 1)[1]
5
votes

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]'
3
votes

As of Python 2.5 there is an even more direct solution. It degrades nicely if the separator is not found:

>>> s = 'Zaptoit:685158:[email protected]'
>>> s.partition(':')
('Zaptoit', ':', '685158:[email protected]')

>>> s.partition(':')[2]
'685158:[email protected]'

>>> s.partition(';')
('Zaptoit:685158:[email protected]', '', '')
1
votes

Following splits the string, ignores first element and rejoins the rest:

":".join(x.split(":")[1:])

Output:

'685158:[email protected]'
0
votes
s = re.sub('^.*?:', '', s)
0
votes

Use the method str.split() with the value of maxsplit argument as 1.

mailID = 'Zaptoit:685158:[email protected]' 
mailID.split(':', 1)[1]

Hope it helped.