Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Length of tab character

Is it possible to type a specific width of tab using \t, or is it a system defined length?

Example code:

print 'test\ttest 2'
like image 956
Colby Gallup Avatar asked Oct 29 '15 17:10

Colby Gallup


People also ask

How long is a tab character?

A Tab is a single character (known as "HT" or ASCII 0x09 or "\u0009" or "\t"). Often when that character is displayed, it is rendered as some ammount of blank area. It has traditionally been rendered as the equivalent of 8 SPACE characters.

Is tab 4 spaces or 8?

Indentation: tabs vs spaces Java: 4 spaces, tabs must be set at 8 spaces.

Is tab equal to 4 spaces?

If you are using something like Notepad, the Tab key will automatically insert four spaces.

How wide is a tab character?

Integers represent the number of space characters (U+0020) that make up the width of the tab character. Negative values are not allowed. Initially the tab size is set to 8 space characters by default.


1 Answers

It is not possible. But, you can replace every tab with custom amounts of spaces using str.expandtabs:

print repr('test\ttest 2'.expandtabs())
# output: 'test    test 2'

print repr('test\ttest 2'.expandtabs(2))
# output: 'test  test 

Edit: note that when using str.expandtabs, the width of tab will depend on where in string the tab is:

print repr('test\ttest 2'.expandtabs(8))
print repr('tessst\ttest 2'.expandtabs(8))
# output: 'test    test 2'
#         'tessst  test 2'

If you want each tab to be replaced by specifyed number of spaces, you can use str.replace:

print repr('test\ttest 2'.replace('\t', ' ' * 8))
print repr('tessst\ttest 2'.replace('\t', ' ' * 8))
# output: 'test        test 2' 
#         'tessst        test 2'
like image 107
GingerPlusPlus Avatar answered Sep 18 '22 18:09

GingerPlusPlus