Search code examples
pythonstrip

`strip` Python method non-obvious behavior


I was surprised about strip Python method behavior:

>>> 'https://texample.com'.strip('https://')
'example.com'

It was not obvious, because usually I use strip with an one-char argument.

This is because of

The chars argument is a string specifying the set of characters to be removed

(https://docs.python.org/3/library/stdtypes.html#str.strip).

What is the best way to delete a "head" of a string?


Solution

  • you have 3 options:

    1. use string.replace instead of string.strip
    2. startswith method:
        if line.startswith("'https://"):
            return line[8:]
    
    1. split:
        if "/" in line:
            param, value = line.split("/",1)