How can I print each individual element of a list on separate lines, with the line number of the element printed before the element? The list information will also be retrieved from a text file.
So far I have,
import sys
with open(sys.argv[1], 'rt') as num:
t = num.readlines()
print("\n"[:-1].join(t))
Which currently gives the read-out:
Blah, blah, blah
etc, etc, etc
x, x, x
But I want this read-out:
1. Blah, blah, blah
2. etc, etc, etc
3. x, x, x
Thanks for you help.
you can use enumerate()
and use str.rstrip()
to remove the all types of trailing whitespaces from the string. and you can also use rstrip('\n')
if you're sure that the newline is going to be \n
only.:
for i,x in enumerate(t,1):
print ("{0}. {1}".format(i,x.rstrip()))