Python Forum
\t produce eight gap but tab only produce four gap
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
\t produce eight gap but tab only produce four gap
#1
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
Reply
#2
I found the answer in another forum, use
print("Hello world\n\tHello world".expandtabs(4))
Reply
#3
I think this is Python default.So,How to change this defualt setting.
Reply
#4
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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Produce One file Per PurchaseOrder jland47 1 328 Jan-26-2024, 11:38 AM
Last Post: Larz60+
  Using multiprocessing to produce objects for i in range lucasrohr 6 1,631 Feb-02-2022, 03:53 PM
Last Post: lucasrohr
  Can the comments produce errors in python? newbieAuggie2019 9 4,265 Nov-26-2019, 12:19 AM
Last Post: micseydel
  Code works in IDLE, appears to work in CMD, but won't produce files in CMD/Windows ChrisPy33 3 3,228 Jun-12-2019, 05:56 AM
Last Post: ChrisPy33
  Python Script to Produce Difference Between Files and Resolve DNS Query for the Outpu sultan 2 2,489 May-22-2019, 07:20 AM
Last Post: buran
  Convert file sizes: will this produce accurate results? RickyWilson 2 8,070 Dec-04-2017, 03:36 PM
Last Post: snippsat
  How can I produce a list of n times the same element? JoeB 6 3,737 Nov-27-2017, 10:40 PM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020