str.strip() strange behavior

前端 未结 5 697
隐瞒了意图╮
隐瞒了意图╮ 2020-11-27 08:25
>>> t1 = "abcd.org.gz"
>>> t1
\'abcd.org.gz\'
>>> t1.strip("g")
\'abcd.org.gz\'
>>> t1.strip("gz")
         


        
5条回答
  •  一整个雨季
    2020-11-27 09:11

    x.strip(y) will remove all characters that appear in y from the beginning and end of x.

    That means

    'foo42'.strip('1234567890') == 'foo'
    

    becuase '4' and '2' both appear in '1234567890'.


    Use os.path.splitext if you want to remove the file extension.

    >>> import os.path
    >>> t1 = "abcd.org.gz"
    >>> os.path.splitext(t1)
    ('abcd.org', '.gz')
    

提交回复
热议问题