Python Split String

2020-02-01 06:59发布

Lets Say we have Zaptoit:685158:zaptoit@hotmail.com

How do you split so it only be left 685158:zaptoit@hotmail.com

7条回答
可以哭但决不认输i
2楼-- · 2020-02-01 07:20

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

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

Output:

'685158:zaptoit@hotmail.com'
查看更多
孤傲高冷的网名
3楼-- · 2020-02-01 07:29
s = re.sub('^.*?:', '', s)
查看更多
仙女界的扛把子
4楼-- · 2020-02-01 07:41
>>> s = 'Zaptoit:685158:zaptoit@hotmail.com'
>>> s.split( ':', 1 )[1]
'685158:zaptoit@hotmail.com'
查看更多
该账号已被封号
5楼-- · 2020-02-01 07:42

Another solution:

s = 'Zaptoit:685158:zaptoit@hotmail.com'
s.split(':', 1)[1]
查看更多
冷血范
6楼-- · 2020-02-01 07:42

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

>>> s = 'Zaptoit:685158:zaptoit@hotmail.com'
>>> s.partition(':')
('Zaptoit', ':', '685158:zaptoit@hotmail.com')

>>> s.partition(':')[2]
'685158:zaptoit@hotmail.com'

>>> s.partition(';')
('Zaptoit:685158:zaptoit@hotmail.com', '', '')
查看更多
冷血范
7楼-- · 2020-02-01 07:43

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

mailID = 'Zaptoit:685158:zaptoit@hotmail.com' 
mailID.split(':', 1)[1]

Hope it helped.

查看更多
登录 后发表回答