[Tutor] Messy - Very Messy string manipulation.
Vusa Moyo
soweto at gmail.com
Thu Oct 29 03:12:55 EDT 2015
Thanks Meenu.
str.translate.
Worked like a charm for python 3.5.
And thanks Alan Gauld for the 2.7 version.
Appreciate the help guys. You guys are awesome.
Regards
Vusa
On Wed, Oct 28, 2015 at 6:23 PM, meenu ravi <meenuravi89 at gmail.com> wrote:
> Hi Vusa,
>
> I was not able to reply through mail list due to some issue. So just
> replying through email.
>
> We can make use of string translate method for more pythonic way. If you
> are using python 2.x, the following itself should work for you:
>
> ***************************************************************************
> import string
> def anti_vowel(str):
> word = str.translate(None, 'aeiouAEIOU')
> return word
>
> print anti_vowel('The cow moos louder than the frog')
> ***************************************************************************
>
> And if you are using python 3.x, "None" inside the str.translate method
> doesn't work. So instead, you can use in this way:
>
> ***************************************************************************
> import string
> def anti_vowel(str):
> word = str.translate(str.maketrans("","","aeiouAEIOU"))
> return(word)
>
> print(anti_vowel("The cow moos louder than the frog"))
>
>
> ***************************************************************************
>
> The above code should work with python 2.x as well with python 2 syntax as
> follows:
>
> import string
> def anti_vowel(str):
> word = str.translate(string.maketrans('', ''), 'aeiouAEIOU')
> return word
>
> print anti_vowel('The cow moos louder than the frog')
>
>
> If you want to know more about translate method, please follow the link,
> https://docs.python.org/2/library/string.html#string-functions
>
> I hope you will get much more options through mailing list.
>
> Happy python:)
>
> Thanks,
> Meenakshi
>
More information about the Tutor
mailing list