0

Given the following code:

l1 = ['001', '002', '003']
print ("ID" + "\t"+ "Usr" + "\t" + "f1" +"\t"+ "f2"+ "\t"+ "f3"+"\n")
print ("12QRS" + "\t"+  "RSW123"+ "".join([ int(z) * "\t" + str(1) for z in l1])+"\n")

output is the following:

12QRS   RSW123  1       1           1

The first two values are separated by one tab. The third, fourth and fifth are separated according to the elements of l1, that is, one, two and three tabs. However, this isn't accurate although the third value is truly separated by one tab, the last two values are not position at the correct place. Their should be positioning at the two and there tabs respectfully. I want to make the last two values to be writing in the correct position?

aBiologist
  • 2,007
  • 2
  • 14
  • 21

1 Answers1

0

Well, first of all, you may have inverted the order of appearance of '\t' and int(z) in your last line of code. I think it should be, according to your intend:

print ("12QRS" + "\t"+  "RSW123"+ "".join([ "\t" * int(z) + str(1) for z in l1])+"\n")

Then, second, why don't you use the formatting functionalities associated with Python strings, the used interpreter being 2.7 or 3.x? Just have a look there for Python 2.7: 7.1. string — Common string operations or even the older syntax: "format spec" % (list of items) which had been discussed there: Python string formatting: % vs. .format

Community
  • 1
  • 1
Schmouk
  • 327
  • 2
  • 6