Why won't this work?

Let’s say I have a list of strings:

list1 = ['Hi', 'how', 'are', 'you1']

Why won’t this work?:

list1[-1].strip('1')

won’t work how? What is you expect to happen that isn’t happening?

strings are immutable, so strip returns a new string, with which you do nothing.

1 Like

it should return ['Hi', 'how', 'are', 'you'], but it stays the same

As @stetim94 said, str.strip() returns a new string. So you must assign that new string to a variable.

Hint: the variable name of the old string can be re-used, since all of the work on the right side of the assignment operator(=) is done before the actual assignment.:

s = 'abc'
s.strip('c')
print(s)
s = s.strip('c')
print(s)

Output:

abc
ab