问题
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'
回答1:
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'
来源:https://stackoverflow.com/questions/33421405/length-of-tab-character