1

I want to learn what is the difference between two code lines.I couldn't find the difference.Whenever I try to run the second code,it doesn't affect string a.

Could someone tell me why the second code line doens't work?

a = "aaaaIstanbulaaaa".strip('a')    #Affects the string
print(a)
>>>Istanbul
a = "aaaaIstanbulaaaa"     #Doesn't affect the string
a.strip('a')
print(a)
>>>aaaaIstanbulaaaa
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
Berke Balcı
  • 81
  • 10

2 Answers2

5

str.strip returns a value; it does not modify the str value invoking it. str values are immutable; you cannot modify an existing str value in any way.

chepner
  • 497,756
  • 71
  • 530
  • 681
2

The str.strip() isn't an inplace method, it doesn't change the current object you're calling with, it returns a new one modified

That is an example of inplace modification

x = [1, 2, 3]
x.append(4)

# x is [1, 2, 3, 4]
azro
  • 53,056
  • 7
  • 34
  • 70