How to write a text starting from a fixed position along a line in txt file in python?

355 Views Asked by At

While writing a txt file using python, say, I have to include "]" at the end of every sentence. I don't know the length of each sentence. But, I have to ensure that "]" is the 50th character in the line. (The length of sentence will be lesser than 50). The text in the file should appear like below:

Apple       ]
Ball        ]
Cat         ]
Dog         ]
Elephant    ]
Frog        ]
1

There are 1 best solutions below

0
On BEST ANSWER

Something like:

lst = ['Bpple','Apple','Ball','Cat','K','ABCDEFGH']
with open('out.txt','w') as f:
  for word in lst:
    f.write(word.ljust(49) + ']\n')

out.txt

Bpple                                            ]
Apple                                            ]
Ball                                             ]
Cat                                              ]
K                                                ]
ABCDEFGH                                         ]