Monday 6 January 2020

string - Detecting Vowels vs Consonants In Python

Change:


if first == "a" or "e" or "i" or "o" or "u":

to:


if first in ('a', 'e', 'i', 'o', 'u'):  #or `if first in 'aeiou'`

first == "a" or "e" or "i" or "o" or "u" is always True because it is evaluated as


(first == "a") or ("e") or ("i") or ("o") or ("u"), as an non-empty string is always True so this gets evaluated to True.


>>> bool('e')
True

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...