I want to have real tabs in a print but \t
only puts spaces.
Eg:
first:ThisIsLong second:Short
first:Short second:ThisIsLong
first:ThisIsEvenLonger second:Short
How would i fix it so i can have all the firsts and all the seconds lined up. Eg:
first:ThisIsLong second:Short
first:Short second:ThisIsLong
first:ThisIsEvenLonger second:Short
In Python strings, the backslash "\" is a special character, also called the "escape" character. It is used in representing certain whitespace characters: "\t" is a tab, "\n" is a newline, and "\r" is a carriage return.
The easiest way to print a tab character in Python is to use the short-hand abbreviation '\t' . To see the tab spaced character in the REPL wrap any variable containing a tab character in the built-in print() function.
Finding the indices of the spaces tell you how many there are so that you can calculate how many tabs there are - a tab is N spaces as defined by the user. You don't need to find the indices to count them; num_spaces = elem. count(' ') works fine.
You can use formatting to align the strings instead. For example, you can tell python that the first column should be 20 characters long, and the second should be 10, left aligned.
For example:
string_one = 'first:ThisIsLong'
string_two = 'second:Short'
print( '{:<20s} {:<10s}'.format(string_one, string_two) )
will print:
first:ThisIsLong second:Short
The first formatting descriptor ({:<20s}
) here is saying:
'<'
left align, 20
at least 20 characters, s
because it's a string
Instead of using tab (\t
), I suggest to use string formatting using printf-style formatting or str.format
:
rows = [
['first:ThisIsLong', 'second:Short'],
['first:Short', 'second:ThisIsLong'],
['first:ThisIsEvenLonger', 'second:Short'],
]
for first, second in rows:
print('%-25s %s' % (first, second))
or
print('{:<25} {}'.format(first, second))
The Python print
function sends strings to standard output. What standard output does with those strings depends on the output device. The default interpretation of \t
is to advance to the next tab stop, which by convention is the character position which is the next multiple of 8 after the position in which \t
occurs (counting character positions from the left beginning with 0).
For example, if I run:
babynames = [('kangaroo', 'joey'), ('fox', 'kit'), ('goose','gosling')]
for x,y in babynames: print(x + '\t' + y)
I get:
kangaroo joey
fox kit
goose gosling
I got the above in IDLE. kangaroo
occupies columns 0-7. \t
is in column 8, hence the next multiple of 8 (the next tab stop) after the tab is in column 16 -- which is indeed where you see joey
. In the next two lines -- the next tab stop after the first word is in column 8. This shows that (at least in the IDLE shell) Python is using real tabs.
Tabs in this sense are somewhat annoying. They can only be used to align variable-length string data with a certain amount of annoying difficulty. As others have indicated the solution is to not use tabs but instead use format
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With