Program to display words from a file that has less than 5 characters

In this python file program, we will display words from a file that has less than 5 characters with the help of the below-given steps. Let’s consider we have readcontent.txt file with the below content.

				
					#readcontent.txt
My name is john.
I'm 20 years old.
I'm learning python at sqatools.
I'm from Australia.

				
			
Steps to solve the program
  1. Open the file by using open(“readcontent.txt”).
  2. Where the file name is the name of the file.
  3. Read the data and split it into words using file.read().split().
  4. Use a for loop to iterate over the words.
  5. If the length of the word is less than 5 then print that word.
  6. Use an if statement for this purpose.
				
					# Open the file
file = open('readcontent.txt')
# Read data and converting it into words
words = file.read().split()
# Iterate over words
for word in words:
# Check for words having length less than 5
    if len(word)<5:
    # Print output
        print(word,end=" ")
				
			

Output:

My name is I’m 20 old. I’m at I’m from

 

 

remove all the lines that contain the character ‘t’ in a file and write it to another file.

replace space by an underscore in a file.

Leave a Comment