Search code examples
pythonstrip

Strip in Python


I have a question regarding strip() in Python. I am trying to strip a semi-colon from a string, I know how to do this when the semi-colon is at the end of the string, but how would I do it if it is not the last element, but say the second to last element.

eg:

1;2;3;4;\n

I would like to strip that last semi-colon.


Solution

  • Strip the other characters as well.

    >>> '1;2;3;4;\n'.strip('\n;')
    '1;2;3;4'