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.
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to