-1
def abc(filename):
    infile = open(filename, 'r+')
    read = infile.read()
    lst = read.split()

    for i in lst:
        if len(i) == 4:
            i.replace(i, 'abcd')
            print(i)

abc('question 4.txt')

I want to replace 4 character strings in my text file but apparently this is not happening. Everything seems to be working fine but the replacing function is not responding.

xSamurai
  • 1
  • 1

1 Answers1

0

This is a possible solution:

 for n, i in enumerate(lst):
    if len(i) == 4:
        lst[n] = 'abcd'
Riccardo Bucco
  • 13,980
  • 4
  • 22
  • 50
rajvi
  • 150
  • 11