[Tutor] Messy - Very Messy string manipulation.
Alex Kleider
akleider at sonic.net
Wed Oct 28 13:07:50 EDT 2015
On 2015-10-28 09:37, Peter Otten wrote:
> Vusa Moyo wrote:
>
>> I've written a script to remove vowels from a string/sentence.
>>
>> the while loop I'm using below is to take care of duplicate vowels
>> found
>> in a sentence, ie
>>
>> anti_vowel('The cow moos louder than the frog')
>>
>> It works, but obviously its messy and n00by. Any suggestions on how I
>> can
>> write this code better?
>
> (I'm assuming Python3)
>
>>>> 'The cow moos louder than the frog'.translate(str.maketrans("", "",
> "aeiouAEIOU"))
> 'Th cw ms ldr thn th frg'
>
I didn't know about the possibility of a third argument. Thanks, Peter.
from the docs:
"""
static str.maketrans(x[, y[, z]])
This static method returns a translation table usable for
str.translate().
If there is only one argument, it must be a dictionary mapping
Unicode ordinals (integers) or characters (strings of length 1) to
Unicode ordinals, strings (of arbitrary lengths) or None. Character keys
will then be converted to ordinals.
If there are two arguments, they must be strings of equal length,
and in the resulting dictionary, each character in x will be mapped to
the character at the same position in y. If there is a third argument,
it must be a string, whose characters will be mapped to None in the
result.
"""
Although not explicitly stated, I assume that if there is a third
argument, the first 2 will be ignored.
More information about the Tutor
mailing list