Python Forum

Full Version: Tab character in a string prints 8 spaces
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I'm starting an online Python class and have been using Python 3 latest with IDLE. I open Python first then open IDLE next to it. Well, I got to the section using the newline and tab characters when printing strings. Then noticed something peculiar on the output. IDLE is configured to treat tabs as 4 spaces, but when the print command runs a string with the \t in it, it shows 8 spaces on the output.

print("Line 1\n\tLine 2")
Output comes out like this:
Output:
Line 1 Line 2
Instead of this:
Output:
Line 1 Line 2
The first thing I did was check IDLE's tab stop configuration and it was already set to 4 spaces. I changed it to 2 spaces but the output still shows 8 spaces.

Thank you for helping in demystifying this issue.
I tried the command line interpreter and got the same results.

Here is another example:
print("12345678\n\tText should start under 5")
Output:
12345678 Text should start under 5
That's just how the Python interpreter handles tabs. For example:

>>> def x(a):
...     if x > 5:
...             do_something()
...
The above was done using tabs. The third line is clearly indented 8 from the second line. The second line looks like it is indented 4 from the first line, but you will note that it is indented 8 from the edge of the screen.
You need to distinguish between A. Tab length when convert tabs in your source code to spaces and B. Tab length when python expand tab escape sequence \t, e.g. when printing string.

A. converting tab to certain number of spaces (default=4) is feature of your IDE (in your case IDLE). Here the more important part is the conversion of tab to spaces (as PEP8 recommends to use spaces for indentation in the source code).

B. If you look at str.expandtabs(tabsize=8) method you will see that default tabsize value is 8. It's safe bet this is default/standard tabsize (i.e. even when not call expandtabs() method).

If you want to get different tab length

>>> print("Line 1\n\tLine 2")
Line 1
        Line 2
>>> print("Line 1\n\tLine 2".expandtabs(4))
Line 1
    Line 2
>>>
If the PEP8 defines the default should be 4 spaces, why is it defaulted to 8 spaces there? I mean its testing anyways, but it does confuse new people as to why they are sometimes getting 8 spaces when they are told they should be getting 4.
(Aug-27-2019, 01:46 PM)metulburr Wrote: [ -> ]If the PEP8 defines the default should be 4 spaces, why is it defaulted to 8 spaces there? I mean its testing anyways, but it does confuse new people as to why they are sometimes getting 8 spaces when they are told they should be getting 4.
I didn't seen it explicitly stated in the docs and it's a guess, but if tabsize in expandtabs() is 8 it will be even more confusing if default is different when expamd \t escape sequence.
(Aug-27-2019, 01:46 PM)metulburr Wrote: [ -> ]If the PEP8 defines the default should be 4 spaces, why is it defaulted to 8 spaces there? I mean its testing anyways, but it does confuse new people as to why they are sometimes getting 8 spaces when they are told they should be getting 4.
It's not Python specific it's just how tabs work Tab key.
They could have changed to 4 to avoid confusion between code indentation(always use 4-space) and output of \t 8-space.
Quote:the horizontal tab size of 8 evolved because as a power of two it was easier to calculate with the limited digital electronics available

When think of of it i never use \t,it always string formatting when i need to move or get a different output.
Also \t can be tricky,it start count at 3 then pushes it up to 8 Confused
>>> print('aaa\taaaaaa\taaa')
aaa     aaaaaa  aaa
A usage i showed in a resent Thread,of course f-string as now always use.
my_string = ("go")
my_list = [1, 2, 3, 4]
for item in my_list:
    print(f'|{my_string} -->{item:4}|')
Output:
|go --> 1| |go --> 2| |go --> 3| |go --> 4|
item:30
Output:
|go --> 1| |go --> 2| |go --> 3| |go --> 4|
Can also solve first task,but not so common usage this and maybe a case when expandtabs(4) can make sense.
Never used expandtabs(4) as if need a other output with string it's always sting formatting(f-string).
>>> print(f"Line 1\n{'Line 2':>10}\n{'Line 3':>14}")
Line 1
    Line 2
        Line 3