How to delete everything after a certain character in a string?

前端 未结 5 1132
無奈伤痛
無奈伤痛 2021-02-13 20:10

How would I delete everything after a certain character of a string in python? For example I have a string containing a file path and some extra characters. How would I delete e

5条回答
  •  有刺的猬
    2021-02-13 20:23

    I think it's easy to create a simple lambda function for this.

    mystrip = lambda s, ss: s[:s.index(ss) + len(ss)]
    

    Can be used like this:

    mystr = "this should stay.zipand this should be removed."
    mystrip(mystr, ".zip") # 'this should stay.zip'
    

提交回复
热议问题