d
WE ARE EXPERTS IN TECHNOLOGY

Let’s Work Together

n

StatusNeo

Python Program to return vowels in a string using Decorators

Scenario-

You are asked to write a program to demonstrate the usage of decorators in which the user will provide input and you are required to create a function that will be decorated in a way such that only vowels in the input are returned.

Solutions-

First Solution-

  • We will create a decorator named a decorator.
  • Then we will create an inner function inside it let’s name it a wrapper.
  • Now, let’s initialize a string consisting of vowels both in the upper and lower case.
  • After that, we will be calling the function that is decorated and is returning the string.
  • We will be using list comprehension to create a new list that will contain only vowels in the string returned by our main function.
  • In the end, we will join the list using join and will return that.

Second Solution-

  • We will create a decorator named a decorator.
  • Then we will create an inner function inside it let’s name it a wrapper.
  • Now, let’s initialize a string consisting of consonants both in the upper and lower case.
  • After that, we will be calling the function that is decorated and is returning the string.
  • In the end, We will use list comprehension inside join and will return the output string comparing with the consonants.

Third Solution-

  • This is the brute force solution
  • All the steps remain the same till calling the main function
  • We will iterate through the input and will check if the word belongs to vowels or not, if not then we will replace the word with space.

References-

Disrupting the Tech World: Product Owner at NerdyBio, Python Powerhouse, AWS Ace & Prolific Tech Blogger 💻💥

Comments

Add Comment