Remove specific characters from a string

前端 未结 2 1915
清酒与你
清酒与你 2020-12-21 12:11

I want to remove all vowels from a string that the user gives. Below is my code and what I get as an output. For some reason the for loop is only checking the first characte

2条回答
  •  [愿得一人]
    2020-12-21 12:53

    That's working as expected. strip is defined as:

    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.

    http://docs.python.org/2/library/stdtypes.html#str.strip

    So as it says, it only affects the leading and trailing characters - it stops looking as soon as it finds a character that isn't in the set of characters to strip. Loosely speaking, anyway; I didn't check the actual implementation's algorithm.

    I think translate is the most efficient way to do this. From the docs:

    >>> 'read this short text'.translate(None, 'aeiou')
    'rd ths shrt txt'
    

    http://docs.python.org/2/library/stdtypes.html#str.translate

提交回复
热议问题