mystack
mystack

Reputation: 5522

Get the string between first two \ and \

i have some strings like

mystrinng=\Mango\stone\forest\
mystrinng=\Orange\sand\house\
mystrinng=\Rabbit\cage\village\

I want to get only the first string

Mango
Orange
Rabbit

I got the strings using the below code.Is there any better way to do this other than the below code

    if(mystrinng.startswith('\\')):
                objname=mystrinng[1:].split('\\')[0]

Upvotes: 0

Views: 46

Answers (1)

Neel
Neel

Reputation: 21317

Try this

In [8]: a = ['\\Mango\\stone\\forest\\', '\\Orange\\sand\\house\\', '\\Rabbit\\cage\\village\\']
In [11]: [x.split('\\')[1] for x in a]
Out[11]: ['Mango', 'Orange', 'Rabbit']

Upvotes: 2

Related Questions