Skip to content Skip to sidebar Skip to footer

Strip Command Doesn't Remove 'e' In The String

I am trying to remove the vowels from the given word and return the word. For e.g. word = 'helleeEoo' If I use the strip command as shown below I am getting an output of 'hell' i

Solution 1:

From the docs (emphasis mine):

Return a copy of the string with the leading and trailing characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a prefix or suffix; rather, all combinations of its values are stripped: ... The outermost leading and trailing chars argument values are stripped from the string. Characters are removed from the leading end until reaching a string character that is not contained in the set of characters in chars. A similar action takes place on the trailing end.

The "e" that isn't being removed by strip isn't at either end of the string. It's in the middle, thus strip won't touch it.

In your second example, the c for c in word is touching every character in the string, thus the middle "e" is touched, tested by the not in, and omitted.

Solution 2:

use a comprehension instead, strip only works at the ends of the string.

''.join(x for x in word if x not in'aeiouAEIOU')

Post a Comment for "Strip Command Doesn't Remove 'e' In The String"