0

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?

Evgenii
  • 3,283
  • 3
  • 27
  • 32
  • Strip strips all of the characters passed, regardless of sequence, which explains the output. The answer below suggesting `replace` is correct. – Simon Crowe Jan 16 '22 at 12:18
  • You might not think the existing behavior is obvious, but the typical usage would be to do things like remove all whitespace from the ends of a string, and for that, you'd want to be able to remove all characters from a specified set. – jamesdlin Jan 16 '22 at 12:19

1 Answers1

1

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)
Tal Folkman
  • 2,368
  • 1
  • 7
  • 21