0

I know there are a few ways to remove vowels, but when I try the method below, the output I get is the string just being printed len(string) times. For example:

s="Labido Labidi"
for i in s:
        if i.lower()=='a' or 'e' or 'i' or 'o' or 'u':    
            s=s.replace(i,"")
            print(s)

The resultant output is:

Labido Labidi
Labido Labidi 
...and so on

What's going on inside the loop ? It isn't even going through the if statement.

Prune
  • 76,765
  • 14
  • 60
  • 81
Koni GTA
  • 23
  • 1
  • 6
  • That's not the actual output. Please edit your post to include the correct output from your code. Also, take a look at [this](https://stackoverflow.com/q/20002503/198633) – inspectorG4dget Jun 09 '17 at 19:08

3 Answers3

2

You are using the or statement incorrectly:

s="Labido Labidi"
for i in s:
    if i.lower()=='a' or i.lower()=='e' or i.lower()=='i' or i.lower()=='o' or i.lower()=='u':    
        s=s.replace(i,"")
        print(s)

You need to put your full evaluational statement after the or

Ryan
  • 1,972
  • 2
  • 23
  • 36
1

The problem is your if logic. After the first or you have to repeat i.lower() == 'e', etc.

Try this:

s="Labido Labidi"
for i in s:
        if i.lower() in 'aeiou':    
            s=s.replace(i,"")
            print(s)
MrJLP
  • 978
  • 6
  • 14
1

The problem is your if condition. or connects two boolean expressions; it doesn't work the same as in English. What you need to check is

if i.lower()=='a' or 
   i.lower()=='e' or
   ...

Better yet, just make a single check on a list of vowels this way:

if lower() in "aeiou":

DETAILS

Any expression used as a Boolean value is evaluated according to whether it is "truthy" or "falsey".  Non-Boolean data types are sometimes not obvious.  In general, zero and `None` values are "falsey", and everything else is "truthy".

Thus, each of the single letters is "truthy", so the Python interpreter regards your if statement as if it says

if i.lower()=='a' or True or True or True or True:

In short, this is always True; your program thinks that everything is a vowel.

Prune
  • 76,765
  • 14
  • 60
  • 81