Python - How to cut a string in Python?

前端 未结 6 1508
失恋的感觉
失恋的感觉 2021-02-05 04:09

Suppose that I have the following string:

http://www.domain.com/?s=some&two=20

How can I take off what is after & includin

6条回答
  •  梦毁少年i
    2021-02-05 04:45

    You need to split the string:

    >>> s = 'http://www.domain.com/?s=some&two=20'
    >>> s.split('&')
    ['http://www.domain.com/?s=some', 'two=20']
    

    That will return a list as you can see so you can do:

    >>> s2 = s.split('&')[0]
    >>> print s2
    http://www.domain.com/?s=some
    

提交回复
热议问题