Reputation: 6364
I have a string say /jrfServer_domain/jrfServer_admin/HelloWorld
, now all I want is HelloWorld
. How can I extract it from such strings ? In this case my delimiter is /
. I'm very new to python.
Upvotes: 2
Views: 1522
Reputation: 34027
Using str.rfind
and slice notation:
In [725]: t='/jrfServer_domain/jrfServer_admin/HelloWorld'
In [726]: t[t.rfind('/')+1:]
Out[726]: 'HelloWorld'
Upvotes: 3
Reputation: 45
>>> s=r'/jrfServer_domain/jrfServer_admin/HelloWorld'
>>> s.split('/')[-1]
'HelloWorld'
maybe you should update your delimiter in your question to "/"
Upvotes: 0
Reputation: 16462
You can use os.path.basename
:
>>> import os
>>> s = '/jrfServer_domain/jrfServer_admin/HelloWorld'
>>> os.path.basename(s)
'HelloWorld'
Upvotes: 0
Reputation: 5275
>>> s = '/jrfServer_domain/jrfServer_admin/HelloWorld'
>>> s.split('/')[-1]
'HelloWorld'
Upvotes: 1
Reputation: 239573
You can use str.rpartition
like this
data = "/jrfServer_domain/jrfServer_admin/HelloWorld"
print(data.rpartition("/")[-1])
# HelloWorld
Upvotes: 2