Reputation: 392
I want to delete the part after the last '/'
of a string in this following way:
str = "live/1374385.jpg"
formated_str = "live/"
or
str = "live/examples/myfiles.png"
formated_str = "live/examples/"
I have tried this so far ( working )
import re
for i in re.findall('(.*?)/',str):
j += i
j += '/'
Output :
live/
or live/examples/
I am a beginner to python so just curious is there any other way to do that .
Upvotes: 3
Views: 3493
Reputation: 10264
You can also use .rindex
string method:
s = 'live/examples/myfiles.png'
s[:s.rindex('/')+1]
Upvotes: 2
Reputation: 1
#!/usr/bin/python
def removePart(_str1):
return "/".join(_str1.split("/")[:-1])+"/"
def main():
print removePart("live/1374385.jpg")
print removePart("live/examples/myfiles.png")
main()
Upvotes: 0