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

前端 未结 5 1137
無奈伤痛
無奈伤痛 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

    str.partition:

    >>> s='abc.zip.blech'
    >>> ''.join(s.partition('.zip')[0:2])
    'abc.zip'
    >>> s='abc.zip'
    >>> ''.join(s.partition('.zip')[0:2])
    'abc.zip'
    >>> s='abc.py'
    >>> ''.join(s.partition('.zip')[0:2])
    'abc.py'
    

提交回复
热议问题