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 usestr.rstrip()to remove the all types of trailing whitespaces from the string. and you can also userstrip('\n')if you’re sure that the newline is going to be\nonly.: