Python Program to Print the Words Starting with s or S from a Text File

Files in Python:

Python file handling is a way of saving program output to a file or reading data from a file. File handling is a crucial concept in the programming world. File management is used in almost every form of project. Assume you are constructing an inventory management system. You have data in the inventory management system related to sales and purchases, thus you must save that data somewhere. Using Python’s file management, you can save that data to a file. You must be given data in the form of a comma-separated file or a Microsoft Excel file if you wish to conduct data analysis. Using file handling, you can read data from a file and write output back into it.

Given a file, the task is to print the words starting with s or S from a given fileĀ in Python.

Program to Print the Words Starting with s or S from a Text File in Python

Below is the full approach for printing the words starting with s or S from a given fileĀ in Python.

Approach:

  • Make a single variable to store the path of the file. This is a constant value. This value must be replaced with the file path from your own system in the example below.
  • Pass the given filename, r (represents read-only) as arguments to the open() function to open the given file.
  • Read the above file using the read() function(get the content) and store it in a variable.
  • Split the above file text lines into a list of words using the split() function and iterate in that list using the for loop.
  • Convert the first character of the word into lowercase using the lower() function and check if it is equal to ‘s’ using the if conditional statement.
  • If it is true, then print that respective word.
  • The Exit of Program.

Below is the implementation:

# Make a single variable to store the path of the file. This is a constant value. 
# This value must be replaced with the file path from your own system in the example below.
givenFilename = "samplefile.txt"
# Pass the given filename, r (represents read only) as arguments to the open() function
# to open the given file
file = open(givenFilename,"r")
# Read the above file using the read() function(get the content) and store it in a variable
filetext = file.read()
print("The words starting with s or S in the given file are:")
# Split the above file text lines into list of words using the split() function and 
# iterate in that list using the for loop
for wrd in filetext.split():
    # Convert the first character of the word into lowercase using the lower() function and 
    # Check if it is equal to 's' using the if conditional statement.
    if wrd[0].lower() == "s":
        # If it is true, then print that respective word
        print(wrd)

Output:

The words starting with s or S in the given file are: 
sample 
specific 
Summary

File Content:

hello this is btechgeeks sample file specific
python codes Summary

Explanation:

  • The file path is stored in the variable ‘file name.’ Change the value of this variable to the path of your own file.
  • Dragging and dropping a file onto the terminal will show its path. The code will not run unless you change the value of this variable.
  • The file will be opened in reading mode. Use the open() function to open a file. The path to the file is the method’s first parameter, and the mode to open the file is the method’s second parameter.
  • When we open the file, we use the character ‘r’ to signify read-mode.
  • The split() method separates all of the words in the given file.
  • We lowercase the initial letter of each word and use an if conditional statement to see if it equals to ‘s‘ and if it does, we print the word.

Google Colab Images:

Files and Code:

Leave a Comment