You can use string formatting to assign your formatted string to a variable and print it
Example using f-strings for python>=3.6
s = f'{fname} [{line_no},{index}] {line}'
print(s)
Or using string.format
s = '{} [{},{}] {}'.format(fname, line_no, index, line)
print(s)
To collect all this in a string via a for loop, you can use a list to collect the strings, and join and print them outside the loop
#List to collect strings
li = []
#Iterate through for loop and add all strings to list
for foo in blah:
s = f'{fname} [{line_no},{index}] {line}'
li.append(s)
#Join list to string and print it
res = ' '.join(li)
print(res)