-1

It's a weird problem

to_be_stripped="D:\\Users\\UserKnown\\PycharmProjects\\ProjectKnown\\PT\\collections\\120"

And two strings below:

s1="D:\\Users\\UserKnown\\PycharmProjects\\ProjectKnown\\PT\\collections\\120\\[Content_Types].xml"
s2="D:\\Users\\UserKnown\\PycharmProjects\\ProjectKnown\\PT\\collections\\120\\_rels\.rels"

When I use the command below:

s1.strip(to_be_stripped)
s2.strip(to_be_stripped)

I get these outputs:

'[Content_Types].x'
'_rels\\.'

If I use lstrip(), they will be:

'[Content_Types].xml'
'_rels\\.rels'

Which is the right outputs. However, if we replace all Project Known with zeus_pipeline:

to_be_stripped="D:\\Users\\UserKnown\\PycharmProjects\\zeus_pipeline\\PT\\collections\\120"

And:

s2="D:\\Users\\UserKnown\\PycharmProjects\\zeus_pipeline\\PT\\collections\\120\\_rels\.rels"

s2.lstrip(to_be_stripped)will be '.rels'

If I use / instead of \\, nothing goes wrong. I am wondering why this problem happens.

1 Answers1

1

strip isn't meant to remove full strings exactly. Rather, you give it a string, and every character in that string is removed from the start and of the string to be stripped.

In your case, the variable to_be_stripped contains the characters m and l, so those are stripped from the end of s1. However, it doesn't contain the character x, so the stripping stops there and no characters beyond that are removed.

Check out this question. The accepted answer is probably more extensive than you need - I like another user's suggestion of using replace instead of strip. This would look like:

s1.replace(to_be_stripped, "")
murchu27
  • 527
  • 2
  • 6
  • 20