8-Bit Borges
8-Bit Borges

Reputation: 10033

Python - splitting strings at second hyphen

I want to follow a pattern of spliting artist - track strings at the '-' separator, like so:

s = 'At the Drive-In - Incurably Innocent'

but, like above, sometimes the artist name has the '-', as well.

if I s.plit('-')[-1], it prints: Incurably Innocent,

but I would like to split only the second '-' ocurrence, and end up with:

['At the Drive-In', 'Incurably Innocent'], using a one-liner.

how do I do it?

Upvotes: 1

Views: 1321

Answers (1)

Moses Koledoye
Moses Koledoye

Reputation: 78554

If the track name is not going to contain any ' - ', you can use str.rsplit on ' - ':

>>> s.rsplit(' - ', 1) # split once
['At the Drive-In', 'Incurably Innocent']

Upvotes: 4

Related Questions