0

I need to use lowercase (a, e, i, o, u) and also uppercase (A, E, I, O, U) in self.all = variable.startswith(('a', 'e' , 'i', 'o', 'u')).

I don't want two variables for Apple and for apple, but always use the same variable x (within which I will manually change Apple or apple).

What I want is to print ok both when I write Apple in x and when i write apple in x. So I would like you to manually replace Apple with apple and try to print ok correctly.

x = "Apple"

class Vocal:
    def __init__(self, variable):
        self.all = variable.startswith(('a', 'e', 'i', 'o', 'u'))
        
vocal = Vocal(x)

if vocal.all:
    print(x, ": ok")
else:
    print(x, ": no")
Ryan M
  • 18,333
  • 31
  • 67
  • 74
  • Do you mean "vowel"? Also, is there a reason you're using a capital letter for what looks like a boolean value? – Mike 'Pomax' Kamermans Apr 01 '23 at 04:48
  • And note that the real answer is "there isn't, use an explicit list if you want to be sure you're testing for those letters" because languages are messy. Using explicit lists is guaranteed to match what you specified as needing to match. Using case conversion is guaranteed to potentially have bugs. – Mike 'Pomax' Kamermans Apr 01 '23 at 05:14

2 Answers2

3

You could convert the string to lowercase before checking.

variable.lower().startswith(('a', 'e', 'i', 'o', 'u'))

Alternatively, you could use a regular expression with the ignore case flag.

bool(re.match('(?i)[aeiou]', variable))
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
  • 's I know, but that's not what I'm looking for. With your code I have lowercase letters, but not uppercase ones – Evangelos Dellas Apr 01 '23 at 04:50
  • @EvangelosDellas What do you mean? This handles uppercase vowels too. Have you tried it? You can see its output here: https://ideone.com/02Nr6U – Unmitigated Apr 01 '23 at 04:50
  • Note that this works 99.99% of the time, but _technically_ fails because languages are hilarious and you can't trust that if you're trying to match uppercase characters, matching var.lower() to a list of lowercase letters yields the same truth table. That's unlikely to be a problem here, but the guaranteed working solution is the one you already have, using the two explicit lists: _there is nothing wrong with having two lists_, the interpreter doesn't care, and being explicit in your code is super in line with good Python code. – Mike 'Pomax' Kamermans Apr 01 '23 at 04:53
1

you can use the .upper() command or .lower() command to convert all characters in the string to upper or lowercase

class Vocal:
    def __init__(self, variable):
        self.Vocal = variable.lower().startswith(('a', 'e', 'i', 'o', 'u'))
Paul
  • 33
  • 6