How to let Python recognize both lower and uppercase input?

前端 未结 6 1852
野性不改
野性不改 2020-12-11 21:28

I am new to Python. I am writing a program that distinguishes whether or not a word starts with a vowel. The problem is, that the program is only able to correctly handle up

6条回答
  •  醉酒成梦
    2020-12-11 21:35

    The check for vowels is done using str.startswith which can accept a tuple of multiple values. PEP 8 Style Guide for Python Code recommends the use of startswith with over string slicing for better readability of code:

    Use ''.startswith() and ''.endswith() instead of string slicing to check for prefixes or suffixes.

    Conditional Expressions are used to set the message indicating whether the word starts with a vowel or not. Then I used the String Formatting method to prepare the message. Also just as a English grammar correction thing I replaced the sentence "The word do not begin with a vowel" with "The word does not begin with a vowel".

    word = input("Please Enter a word:")
    is_vowel = 'does' if word.lower().startswith(tuple('aeiou')) else 'does not'
    print("The word {} begin with a vowel".format(is_vowel))
    

提交回复
热议问题