Delete Chars in Python

后端 未结 4 619
清歌不尽
清歌不尽 2021-01-22 02:12

does anybody know how to delete all characters behind a specific character??

like this:

http://google.com/translate_t

into

<         


        
4条回答
  •  猫巷女王i
    2021-01-22 02:20

    If you know the position of the character then you can use the slice syntax to to create a new string:

    In [2]: s1 = "abc123"
    In [3]: s2 = s1[:3]
    In [4]: print s2
    abc
    

    To find the position you can use the find() or index() methods of strings. The split() and partition() methods may be useful, too. Those methods are documented in the Python docs for sequences.

    To remove a part of a string is imposible because strings are immutable.

    If you want to process URLs then you should definitely use the urlparse library. It lets you split an URL into its parts. If you just want remove a part of the file path then you will have to do that still by yourself.

提交回复
热议问题