-4

I'm trying to remove or ignore the ' symbol (Apostrophe) within a list of strings. I don't know if my for loop is just plain wrong or what:

n = ["a", "a's", "aa's"] #example list

for i in n:
    i.strip("'")
khelwood
  • 55,782
  • 14
  • 81
  • 108
Blue B
  • 3
  • 1
  • 6
    `.strip` only removes from the start and end of the string, [as documented](https://docs.python.org/3/library/stdtypes.html#str.strip). Also note that it returns a new string, so the list wouldn't have changed anyway. – jonrsharpe Dec 04 '18 at 10:51
  • 1
    Also calling`strip` achieves nothing unless you do something with its return value. – khelwood Dec 04 '18 at 10:53
  • 2
    just do `n = [x.replace("'", "") for x in n]`. `strip` does not work here for the reasons explained in the above comments. – Ma0 Dec 04 '18 at 10:53

2 Answers2

1

strip won't work here use replace,

In [9]: [i.replace("'",'') for i in lst]
Out[9]: ['a', 'as', 'aas']
Rahul K P
  • 15,740
  • 4
  • 35
  • 52
1

Two problems here.

  • First, strip doesn't work in the middle of the string, you have to use `replace("'", "")
  • Second, and more important, strings are immutable. Even if i.strip(...) did what you want, it would not change i. It would just produce a new string. So, you have to store that string.

Sum up, try something like

n = [i.replace("'", "") for i in n]
blue_note
  • 27,712
  • 9
  • 72
  • 90