Search code examples
pythonstring

How to remove a last word in the string in Python?


For example if I have a string like this:

a = "[email protected]:/home/hello/there"

How do I remove the last word after the last /. The result should be like this:

[email protected]:/home/hello/ 

OR 

[email protected]:/home/hello

Solution

  • Try this:

    In [6]: a = "[email protected]:/home/hello/there"
    
    In [7]: a.rpartition('/')[0]
    Out[7]: '[email protected]:/home/hello'