1

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" instead of "hll"

word = word.strip("aAeEiIoOuU")

But if I use the join command as shown below it works fine:

word = ''.join(c for c in word if c not in 'aAeEiIoOuU')

I am using python 3 and I would like to know why in the case of the strip command 'e' is present in final output?

CDspace
  • 2,639
  • 18
  • 30
  • 36
codingfreak
  • 4,467
  • 11
  • 48
  • 60
  • 5
    Because the "e" that's left isn't at either the beginning or the end of the string. It's protected by the "h" and the "ll". – kindall Apr 18 '17 at 17:28
  • 1
    The strip method just strips chars from the start or end of the string. Take a look at translate. – PM 2Ring Apr 18 '17 at 17:30
  • @kindall .. thanks for the update .. I was in the perception that strip will remove all the character instances in string rather than Just the First one and Last one. In my example since trailing O is removed even the middle 'e' is also removed along with it. – codingfreak Apr 18 '17 at 17:43
  • 2
    Whatever documentation gave you that idea, throw it away... – kindall Apr 18 '17 at 17:57
  • BTW, it's better to pass `.join` a list comprehension than a generator expression. If you pass `.join` an iterator it has to create a list from it because it needs to scan the strings twice. On the 1st scan it calculates the total string length so it can allocate the destination string, and then it does a 2nd scan to copy the chars to the destination. [This answer](http://stackoverflow.com/a/9061024/4014959) by Python core developer Raymond Hettinger shows the speed difference. – PM 2Ring Apr 19 '17 at 16:14

2 Answers2

6

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.

CDspace
  • 2,639
  • 18
  • 30
  • 36
1

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

''.join(x for x in word if x not in 'aeiouAEIOU')
Haleemur Ali
  • 26,718
  • 5
  • 61
  • 85