0

Like in the following code; how do I avoid repeating "char =="?

for char in s:
    if char == 'a' or char == 'e' or char == 'i' or char == 'o' or char == 'u':
        do... 

Is it possible to approximate the second line to what one would say in natural language: "if char is equal to a, e, i, o, or u..." ?

Martin
  • 414
  • 7
  • 21

1 Answers1

1

You can use the in-operator to check if a char exists in a string:

if char in "aeiou":
    #Do something
Cleared
  • 2,490
  • 18
  • 35
  • So you can make the characters into a string and not only avoids repeating the variable, but also " == " and "or"! Supernice and simple! – Martin Nov 30 '16 at 12:26