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'
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.
Indentation: tabs vs spaces Java: 4 spaces, tabs must be set at 8 spaces.
If you are using something like Notepad, the Tab key will automatically insert four spaces.
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.
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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With