Python Program To Print Words Having More Than 5 Characters
Maybe your like
Python Program to Print Words Having more Than 5 Characters
Q. Write a program to print words having more than five characters in a text file in python.
file: “text.txt” 
Solution:
# print words having len 10 or more def read_data(): f = open("text.txt", 'r') s = f.read() x = s.split() for i in x: if len(i) > 5: print(i) read_data()Output:
# sample # produce. # purchase, # because # purchased # something # deliver. # please # samples # you've # converted? # results. # Thanks!
Explanation: Here we have defined a function read_data(). Inside read_data() function we have also created a file object “f” and opened our text file in read mode ie. “r” mode.
In next step, we have initialized all the values to variable “s“. And used read() function to read our file word by word. The read() function return the output in string format.
Note: Here inside function, we have used split() function which returns the list of words available in our text file. Here “s” is our file object.
split() method will separate each word from space and store them in list “x“. After that we used for loop to iterate over each word present inside our list “x” and checked for the length of each word using len() function. If word with more than 5 characters length found then it will be printed and rest will be skipped. Hence we got the final result.
-
Programming questions on Text Files
- WAP to define a method to read text document line by line.
- WAP to define a method in python to read lines from a text file starting with an alphabet F.
- WAP to define a method to count number of lines starting with an alphabet F.
- WAP to define a method which display only those lines starting with an alphabet A or F.
- WAP to define a method to display only those lines which are bigger than 50 characters.
- WAP to define a method to count total number of characters in our text file.
- WAP to define a method to count total numbers of word available in our text file.
- WAP to define a method which counts the occurrence of particular word in a text file.
- WAP to define a method which counts the occurrence of “is”, “to” in a text file.
Programming questions on Binary Files
- WAP to define a method which displays the records of student having marks between 50 & 70.
- WAP to define a method which displays only those student records who secured grade A.
Tag » How To Print Words Python
-
Print() And Standard Out
-
How Do You Print A Word In Python? - Quora
-
Python Program To Print Even Length Words In A String - GeeksforGeeks
-
How Do I Print A String One Word At A Time In Python? - Stack Overflow
-
Your Guide To The Python Print() Function
-
How To Print Words In Python - Using Print Function - YouTube
-
Python Print Variable – How To Print A String And Variable
-
Python Print() Function - W3resource
-
Python: Convert The String To A List And Print All The Words And Their ...
-
Python Program To Print Even Length Words In A String - Tutorialspoint
-
Printing Words Vertically In Python
-
Python Print() Function: How To Use Print Statement With Examples
-
Python Print() - DigitalOcean
-
Print Words Vertically - LeetCode