In this article we will show you the solution of find vowels in string python, to find vowels in a string in Python is similar to a searching method in Data structures and Algorithms. We can perform various actions on a string as per program requirements.
This is not limited to finding vowels or certain characters in a string, we can also find substrings, numbers, alphanumeric combinations, and much more.
All this can be done with simple programming fundamentals, as shown below.
Step By Step Guide On Find Vowels In String Python :-
To find the vowels in a string and print them in Python, we are going to use the built-in lower() function and print() function along with the extra end=" " parameter.
Use: The lower() function converts all the characters in a string into lowercase of the alphabet.
Syntax: print(object, end=" ")
Use: The print() function prints or displays the output in the console.
The extra 'end' parameter is used to keep the cursor on the same line.
If we pass a space through the 'end' parameter then each print statement's output is separated by a space and not a new line.
Similarly, if we pass forward slash or any other character then the output will be separated by that character.
str1 = 'Python is a dynamic language.' vowels = ['a', 'e', 'i', 'o', 'u'] for i in str1: if i.lower() in vowels: print(i , end=" ") #Code Output o i a a i a u a e
- A string is defined as str1, the string can also be taken as input from the user.
- All the vowels are stored in a list named 'vowels', which can be further used for finding the vowels in the string.
- To iterate over every character in a string to check if it's a vowel or not, we will use a 'for-loop'.
- During iteration built-in lower() function is used to convert each character into lowercase. As the vowels stored in the 'vowel' list are in lowercase, this increases the simplicity of the program.
- Using the if conditional statement, the character is checked to be present in the list of vowels or not.
- If the character is present, then it is printed in the console.
- In the above code snippet, 'Python is a dynamic language.' is the defined string.
- After iterating through the characters in the string. Using an if conditional statement, if the character is a vowel, directly print it.
- If the character is a consonant no action is specified so none will be taken.
- To make sure that the output is on the same line, the end parameter is used and passed the value of a space.
- When the iteration is over, we get all the vowels in a string in Python
This is how we can find vowels in a string and print them in Python using built-in functions and loops.
There are other ways too to find the vowels in string in Python, you can try them out.
I hope this article on find vowels in string python helps you and the steps and method mentioned above are easy to follow and implement.