Search code examples
pythonsplitfilenamesstriprenaming

Python renaming strings


Kinda dumb question but I got stuck. So I got this

print (image_files[index].split(".")[0]).split("/")[1].split

which gives the following output:

sun5_face_righteye

How can I keep stripping or splitting the string so I get only:

righteye

Thank you


Solution

  • you can try:

    >>> s = "sun5_face_righteye"
    >>> s.split("_")[2]
    'righteye'
    

    Edit1:

    >>> s = "xxxxx-face-_tracker_face_righteye"
    >>> s.split("_")[-1]
    'righteye'
    

    Edit2 :

    >>> s = "sun5_face_righteye"
    >>> s.split("_face_")[0]
    'sun5'
    >>> s = "57268-face-_tracker_face_lefteye"
    >>> s.split("_face_")[0]
    '57268-face-_tracker'