Python Program to print the words starting with a vowel from a text file

Python Program to print the words starting with a vowel from a text file

In this article, we will be solving question based on data file handling in Python. According to the question we have to print all the words starting with a vowel from a text file.

If you like the article content, then share it in your coding groups so that more people can take advantage of the content. So, without wasting any time let's solve this question.

Question - Write a Python Program to print the words starting with a vowel from a text file

Python Program to print the words starting with a vowel from a text file


Logic and Explanation

Question - Write a Python Program to print the words starting with a vowel from a text file

Python Program to print the words starting with a vowel from a text file - In this program we have to print all the words present in the text file which starts from a vowel. So for the above problem, we first have to open the text file in read mode in our python program. In my example, I am taking a text file name "content.txt" which is present in the same directory where this python file is present. So, I just written the name of file otherwise you have to write the complete path of the file to open the file. This is the code to open file in read mode.


f = open("content.txt","r")


Now, we will read the content of the file by iterating through the content of the file word by word. For this we will first read the whole text file in the form a string. Here, I am reading the text file using f.read() function and storing the text inside the file in a string variable "data".

data = f.read()

Now, for iterating word by word by we will use the below command. Here, we split the data string with spaces. We can split with any string by default it is set to space.


for word in data.split():

Now we will check for the condition that whether the words starts from a vowel or not. First we will access first letter of word using index zero. Then, we will lower that letter and we will check whether it is in the tuple of lowercase vowels or not.  If yes, then we will print that word.


if word[0].lower() in ("a","e","i","o","u"):
    print(word)

Complete source code the question is given below. I hope the explanation and the logic of the program is clear to you. In case of any doubt, you can ask in the comments below. Here's the complete code for the problem.

f = open("content.txt","r")
data = f.read()
for word in data.split():
    if word[0].lower() in ("a","e","i","o","u"):
        print(word)


So, I hope you liked the article and you would have found the content of the article useful and helpful for you. Share this article in your coding communities so that more people can take advantage of the content. Also, In case of any doubt feel free to ask in the comments below.

For More Such Python Programs - Click Here

Enjoy Coding!!!


Also Read :-

Extract Phone Number Details Using Python





1 Comments

For any doubts feel free to ask in comments below.
Stay Connected to Us for more such Courses and Projects.

Post a Comment

For any doubts feel free to ask in comments below.
Stay Connected to Us for more such Courses and Projects.

Post a Comment

Previous Post Next Post