How to put uppercase and lowercase letters in the same variable without retyping them twice?
Question:
I would 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 SPECIFY that 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).
I searched other questions on Stackoverflow, but there were only solutions with 2 variables/strings and that’s not what I’m looking for, so this question is not duplicate.
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")
Answers:
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))
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'))
I would 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 SPECIFY that 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).
I searched other questions on Stackoverflow, but there were only solutions with 2 variables/strings and that’s not what I’m looking for, so this question is not duplicate.
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")
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))
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'))