rstrip not removing newline char what am I doing wrong? [duplicate]

The clue is in the signature of rstrip.

It returns a copy of the string, but with the desired characters stripped, thus you’ll need to assign line the new value:

line = line.rstrip('\n')

This allows for the sometimes very handy chaining of operations:

"a string".strip().upper()

As Max. S says in the comments, Python strings are immutable which means that any “mutating” operation will yield a mutated copy.

This is how it works in many frameworks and languages. If you really need to have a mutable string type (usually for performance reasons) there are string buffer classes.

Leave a Comment