Python Forum
Tab character in a string prints 8 spaces
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tab character in a string prints 8 spaces
#4
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 you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
RE: Tab character in a string prints 8 spaces - by buran - Aug-27-2019, 06:16 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Split string into 160-character chunks while adding text to each part iambobbiekings 9 9,933 Jan-27-2021, 08:15 AM
Last Post: iambobbiekings
  Help with removing spaces and tabs from a string msqpython 14 4,347 Jan-21-2021, 10:48 PM
Last Post: deanhystad
  Replacing a few characters of a specified character but not all of them from a string fatherted99 7 3,541 Aug-13-2020, 09:08 AM
Last Post: fatherted99
  not find the resone it not prints yukhei 3 1,864 May-22-2020, 03:36 PM
Last Post: deanhystad
  Finding and storing all string with character A at middle position Pippi 2 2,795 Jan-20-2019, 08:23 AM
Last Post: Pippi
  Unexpected character after line continuation character joshyb123 5 10,859 Sep-05-2018, 08:08 AM
Last Post: perfringo
  unexpected character after line continuation character error newbie 10 15,013 Aug-09-2018, 06:07 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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