Reputation: 43
Why do I get different amounts of spaceing between each \t and how would I go about fixing this?
print ("\tName1:\tName2:\tNumber1:\tNumber2:")
Output:
Name1: Name2: Number1: Number2:
The space between Number1: and Number2: is not the same as the first two; how would I go about fixing this?
Upvotes: 0
Views: 950
Reputation: 19144
Here is a simple way to put n
spaces between each string.
>>> n = 3
>>> print((' '*n).join(('Name1:', 'Name2:', 'Number1:', 'Number2:')))
Name1: Name2: Number1: Number2:
Upvotes: 0
Reputation: 77837
\t advances to the next tab stop; this is usually set to the next multiple of 8 columns. In other words, it's not broken: it's working just as it's supposed to.
If you want the same quantity of spaces between your labels, then use a fixed number of spaces, rather than tabs. For instance:
space = " "
print "Name1" + space +
"Name2" + space +
"Name3" + space +
"Name4"
You can also use formatted output.
Upvotes: 0
Reputation: 36608
The console has tab-stops at every 8 spaces. If you are passed a tab-stop, it moves to the next one. You can see where the tab-stops are here:
print("\tName1:\tName2:\tNumber1:\tNumber2:")
print('1234567890'*5)
print('\t^'*5)
Name1: Name2: Number1: Number2:
123456781234567812345678123456781234567812345678
^ ^ ^ ^ ^
It makes sure that at least 1 space still exists after a tab. That is why Number2:
got moved to the next tab-stop.
If you want to print with a pre-fixed spacing, you can do it like this:
header = ['Name1:', 'Name2:', 'Number1:', 'Number2:']
# set the width of the headers as the widest string length plus 4
width = max(len(h) for h in header) + 4
print('{: <w}{: <w}{: <w}{: <w}'.replace('w',str(width)).format(*header))
# output:
Name1: Name2: Number1: Number2:
The print statement is a bit confusing, but it sets the number of spaces to pad each string with as width
.
Upvotes: 1