Python Forum
\t produce eight gap but tab only produce four gap - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: \t produce eight gap but tab only produce four gap (/thread-18994.html)



\t produce eight gap but tab only produce four gap - liuzhiheng - Jun-09-2019

The code is:
print("Hello world\n\tHello world")
The result is:
Output:
Hello world Hello world
but I want this \t result
Output:
Hello world Hello world



RE: \t produce eight gap but tab only produce four gap - Gribouillis - Jun-09-2019

I found the answer in another forum, use
print("Hello world\n\tHello world".expandtabs(4))



RE: \t produce eight gap but tab only produce four gap - liuzhiheng - Jun-09-2019

I think this is Python default.So,How to change this defualt setting.


RE: \t produce eight gap but tab only produce four gap - Gribouillis - Jun-09-2019

I don't think it is a python setting. The call to print sends tab characters to the output stream. If the console prints 8 spaces when it receives a tab character, it doesn't have anything to do with python, unless you are running the code in a python IDE such as Idle. In my bash terminal, if I write a C program that prints the same output, it also inserts 8 spaces.

Same problem with bash's echo command
Output:
λ echo -e 'foo\n\tbar' foo bar
Edit: Actually, I've found a solution for the linux terminal here. It suffices to type tabs -4 to change the terminal's setting. For example
Output:
λ tabs -4 λ echo -e 'foo\n\tbar' foo bar λ python3 -c "print('foo\n\tbar')" foo bar
It Worked!