Python vowel check [duplicate] - python

This question already has answers here:
Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?
(8 answers)
Closed 5 years ago.
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.

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

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)

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.

Related

Why does "x and y" return "y" when "x" and "y" are integers? [duplicate]

This question already has answers here:
Python's Logical Operator AND [duplicate]
(7 answers)
Closed last year.
So I came across the following syntax for a coding-game solution:
.
.
n and 9
and I didn't knew how to interpret it, thus I tried
2 and 3 #3
3 and 2 #2
.
Why does x and y (seems to) equal y i.e how is this calculated/understood?
This is called short-circuiting in boolean expressions if the first is true(non zero considered as true) it goes to 2nd if this was or if the first was false(0 is considered false) it goes to 2nd try it or as or integers hope this clears stuff up
Chain of logical operators returns the first (counting from left-most value) item that lets you know the logical value of the whole statement. So, in case of and, there are 2 options - one of the values is False, at which point you know that whole statement is False and you return that value without checking anything further on the right, or all of them are True and you return the last one, as only then you know that it will be True.

False when check for upper later in python [duplicate]

This question already has answers here:
How can I test if a string starts with a capital letter? [duplicate]
(3 answers)
Closed 2 years ago.
I need to check if the first letter of a list is upper. For that I wrote this simple code, where obviasly my word "Try" starts with capital "T":
h=[]
h.append("Try")
a = str(h[0])
print(a)
print(a.isupper())
BUT when I print a.isupper I get always False. Should I convert the variable in something, or it should be str object? How can I solve this problem
You dont need the list. Just do:
a= "Try"
print(a[0].isupper())
You are checking the whole string to be capital, that's why it's False.
When you do print(a[0].isupper()), it checks if the whole string(Try in your case) is capital. Hence, it returns False.
You want to check just the first letter of the string, so do this instead:
In [615]: print(a[0].isupper())
True
Where a[0] gives you T.
You are using h[0] which gives "Try", and when you check for a.isupper()
It has both lower case and upper case , please check for a[0] , then you get true if the first letter is capital

How does the count function count? [duplicate]

This question already has answers here:
Why are str.count('') and len(str) giving different output?
(3 answers)
How does the count() method work? [duplicate]
(1 answer)
Closed 4 years ago.
This is a short one, yet very irritating. I know I can count the amount of times a string occurs within another string like this:
'banana'.count('a')
>>>3
meaning that banana contains the letter "a" 3 times.
This is where it gets kind of weird.
My first confusion is - when I do 'foo'.count(''), what does Python look for?
is '' == None == anything?
It doesn't seem to be the case, but then again, what IS '' logically speaking? And more importantly, why does
'test'.count('')
>>>5
return one more than the length of the string?
What the hell is included in a string that's always 1 higher than the amount of letters? the void?
EDIT: the ' character twice looks like one " character. I am talking about two times ' here, to avoid confusion
EDIT2: There seems to be some confusion about how the amount of '' happen. Refer to comments below.
Every string1 can be thought of as:
any_string = "" + "".join(any_string) + ""
which contains exactly len(any_string) + 1 instances of ''.
For "foo" for example, it would be:
"" + "f" + "" + "o" + "" + "o"+ ""
# |----- from join -------|
As it can be seen there are 4 instances of "" in it.
Note however, that this is a problem where no answer or all answers could somehow support a case for themselves. It get's philosophical:
How much nothing is contained in nothing?
How much nothing is contained in something?
This answer tries to explain the convention used by Python and does not intend to suggest that this is the way all languages do it \ should be doing it; it is just how Python does it.
1Empty strings are an exception and are handled differently; they simply return 1; which is yet another convention.
str.count(sub)
Counts the number of occurrences of sub in str.
Since strings are sequences, it basically counts the number of splits sub would cause in str.
An empty string is at the beginning, between each character, and at the end.
Hence, why when you use 'test', which has a len of 4, you get 5 occurrences of sub ('').

Python: Shorter way of checking if a string equals two other strings [duplicate]

This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 6 years ago.
have just started learning python and this question came up to my mind
is there a shorter way to determine if a string equals 'something' or 'somethingelse'?
example:
input = raw_input("question?")
while input != 'n' and input != 'y':
#ask question again
You could check whether it is in a list or set.
input = raw_input("question?")
while input not in ['n', 'N']:
#ask question again
If you are just trying to accept two cases though, you could also just call lower on the input.
while input.lower() != 'n':
#ask question again
Perhaps unexpectedly, 'a' != input != 'b' works. It resolves the same way as ('a' != input) and ('b' != input). You can do the same thing with ==, or with <, >, etc. on numbers, as well.
Oh, but you have to be careful if you chain it longer than three things, or use multiple different comparison operators.
If it's a case issue, then you can:
while input.lower() != 'n'

Finding if letter = another letter in python [duplicate]

This question already has answers here:
invalid syntax on =? [closed]
(2 answers)
Closed 8 years ago.
This is in python and I'm having a little trouble finding this out, I put.
s = 'goodbye'
and I want to know if the first letter is a g.
so i put
s[0] = 'g'
but i get an error, what is the right way to finding this?
A single = means 'assignment', and doing two == means 'compare and see if they're equal'. The difference between the two can be subtle (just a single character difference!), so make sure you don't get confused between the two
You want s[0] == 'g':
if s[0] == 'g':
print "word starts with 'g'"
Doing s[0] = 'g' is telling Python "change the first letter of the string to 'g'". However, that fails because in Python, strings are immutable -- they can never be changed.
You could use the startswith(prefix) method (returns True if string starts with the prefix, otherwise returns False):
>>> s = 'hello'
>>> a = s.startswith('h')
>>> a
True

Categories