Python Workout 11: String Logic

Level of Difficulty:

Objective: This workout provides practice in creating functions utilizing conditional logic with strings.

Problem: Create a function that takes in a string as an input. If the string starts with a vowel, return True. If not, return False.

Examples:

starts_with_vowel(‘hello’) → False

starts_with_vowel(‘another’) → True

Submission

SImply post your code and a screenshot of your results.

Please format your Python code and blur it or place it in a hidden section.

This workout will be released on Monday April 11, 2023, and the author’s solution will be posted on Sunday April 16, 2023.

Hi ,

My submission for this :slight_smile:

image

Code

image

Thanks

2 Likes

Great job @Anurag

Here is my solution

def starts_with_vowel(word):
    vowels = ['a', 'e', 'i', 'o', 'u']
    return word[0].lower() in vowels

Bit longer solution:
def starts_with_vowel(strtext):
vowels = [“a”,“e”,“i”,“o”,“u”]
first_letter = strtext[0].lower()
is_vowel = False
for v in vowels:
if v == first_letter:
is_vowel = True
return is_vowel

starts_with_vowel(‘ello’)
starts_with_vowel(‘xllo’)

Here is my solution in Python:

def starts_with_vowel(word):
    vowels = ['a', 'e', 'i', 'o', 'u']
    if word[0].lower() in vowels:
        return True
    else:
        return False

This function takes in a word as an input parameter. It then checks if the first letter of the word (converted to lowercase) is present in the list of vowel letters. If it is, the function returns True, indicating that the word starts with a vowel. Otherwise, it returns False.

The bottom example here

Loving this workout. Nice work!