To explain my query I have a simple code snippet below followed by my question.
def count_vowels(s):
num_vowels = 0
for char in s:
if char in 'aeiouAEIOU':
num_vowels = num_vowels + 1
return num_vowels
print(count_vowels(""))
print("" in "aeiouAEIOU")
gives an output
0
True
My doubt:
Why does an empty string "" returns True for the expression
"" in "aeiouAEIOU"
But it skips when it is present along with a for loop?
for char in s:
My understanding is that empty strings are a subset of all strings then why it is ignored when the same expression is in the for loop? Feel free to correct me if there is something I am missing here.
count_vowelsis exited immediately, eh?strobjects have the strange characteristic that iterating over them produces objects of that same type, since python does not have a "char" type, it is simply length-1 strings.True. When you evaluate it inside a body loop you don't evaluate it once as the body is never entered as the string iterated over is empty.