Python split a set of characters by pointing a particular string

Viewed 42

The input I got:

data = 'VALUE - TRIP NAME: LOSANGELES45 US - UNITED STATES-9876543210:TRIP NAME: LOSANGELES45'

Expected output:

result = LOSANGELES45

I need to print out the LOSANGELES45 from the above string. The TRIP NAME will be a static value and the LOSEANGELES45 will be a dynamic value. How can I achieve this in python 3.7?

2 Answers
data.split("TRIP NAME: ")[1].split(" ")[0]

This splits your string at 'TRIP NAME: ', selects the second element (from LOSANGELES45 onward), splits the string again after a space and selects the first element.

you can try the below split and select the method. It might help for given problem

data.split(' ')[-1]

as 'LOSEANGELES45' is always in last. by [-1], it always selects the last splited string

Related