Write a python program to read a text file 'story.txt' line by line and display each word separated by '#'.

Understand the Problem

The question is asking us to create a Python program that reads a specified text file, 'story.txt', and processes its content line by line. For each line, we need to display each word separated by a '#' character. This involves file handling and string manipulation in Python.

Answer

Open 'story.txt' and split each line into words, separated by '#'.
with open('story.txt', 'r') as file:
    for line in file:
        words = line.split()
        formatted_line = '#'.join(words)
        print(formatted_line)
Answer for screen readers
with open('story.txt', 'r') as file:
    for line in file:
        words = line.split()
        formatted_line = '#'.join(words)
        print(formatted_line)

More Information

This program reads a text file line by line, splits words, and combines them using '#' as a separator, demonstrating basic file operations and string manipulation in Python.

Tips

Avoid forgetting to remove new line characters before splitting words. Use strip() if necessary to avoid unexpected empty entries.

AI-generated content may contain errors. Please verify critical information

Thank you for voting!
Use Quizgecko on...
Browser
Browser