制表符长度 [英] Length of tab character
本文介绍了制表符长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以使用 \t
输入特定宽度的制表符,还是系统定义的长度?
Is it possible to type a specific width of tab using \t
, or is it a system defined length?
示例代码:
print 'test\ttest 2'
推荐答案
这是不可能的.但是,您可以使用 str 用自定义数量的空格替换每个选项卡.expandtabs
:
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
编辑:注意在使用 str.expandtabs
时,标签的宽度将取决于标签在字符串中的位置:
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'
如果您希望每个选项卡都被指定数量的空格替换,您可以使用 str.replace
:
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'
这篇关于制表符长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文