Friday, 29 April 2011

python script(program) for reversing every line in a file

hi,

I was given some exercises in python, today i tried some of them first program i tried was to reverse every line in a file and print them.

I finished the exercise, the program is

f = open('words.txt')
while True:
    line = f.readline().rstrip('\n')
    if len(line) == 0 :
        break
    else:
        print line[::-1]

f.close()

i/p(words.txt):

this is a text string
this is another string

o/p:

gnirts txet a si siht
gnirts rehtona si siht



No comments:

Post a Comment