Read a text file line by line and display each word separated by a #.
def process_line(line): word = '' for c in line: if c.isalpha(): word += c else: if word: print(word, end='#') word = '' if word: print(word, end='#') print() with open('filename.txt') as infile: for line in infile: process_line(line)