Remove leading and trailing slash / in python

前端 未结 3 629
囚心锁ツ
囚心锁ツ 2021-02-01 11:47

I am using request.path to return the current URL in Django, and it is returning /get/category.

I need it as get/category (witho

3条回答
  •  一向
    一向 (楼主)
    2021-02-01 12:22

    def remove_lead_and_trail_slash(s):
        if s.startswith('/'):
            s = s[1:]
        if s.endswith('/'):
            s = s[:-1]
        return s
    

    Unlike str.strip(), this is guaranteed to remove at most one of the slashes on each side.

提交回复
热议问题