如何以固定宽度打印字符串?

190 投票
8 回答
427043 浏览
提问于 2025-04-17 08:04

我有一段代码(用来打印一个字符串中所有排列的出现次数)

def splitter(str):
    for i in range(1, len(str)):
        start = str[0:i]
        end = str[i:]
        yield (start, end)
        for split in splitter(end):
            result = [start]
            result.extend(split)
            yield result    

el =[];

string = "abcd"
for b in splitter("abcd"):
    el.extend(b);

unique =  sorted(set(el));

for prefix in unique:
    if prefix != "":
        print "value  " , prefix  , "- num of occurrences =   " , string.count(str(prefix));

我想打印出字符串变量中所有排列的出现次数。

因为这些排列的长度不一样,所以我想固定宽度,打印得好看一点,而不是像这样:

value   a - num of occurrences =    1
value   ab - num of occurrences =    1
value   abc - num of occurrences =    1
value   b - num of occurrences =    1
value   bc - num of occurrences =    1
value   bcd - num of occurrences =    1
value   c - num of occurrences =    1
value   cd - num of occurrences =    1
value   d - num of occurrences =    1

我该如何使用 format 来实现呢?

我找到了这些帖子,但它们对字母数字字符串的处理不太好:

python 字符串格式化固定宽度

用 python 设置固定长度

8 个回答

113

最开始是作为对@0x90回答的补充,但因为偏离了原帖的意图被拒绝了,所以建议作为评论或回答发布,所以我在这里简单写一下。

除了@0x90的回答之外,语法可以更灵活一些,比如使用一个变量来表示宽度(正如@user2763554的评论所提到的):

width=10
'{0: <{width}}'.format('sss', width=width)

此外,你还可以让这个表达式更简洁,只用数字,并依赖于传递给format的参数顺序:

width=10
'{0: <{1}}'.format('sss', width)

甚至可以完全不使用数字,以达到最大程度的紧凑性,这可能不是很符合Python的风格:

width=10
'{: <{}}'.format('sss', width)

更新于2017-05-26

在Python 3.6中引入了格式化字符串字面量(简称“f-strings”),现在可以用更简洁的语法访问之前定义的变量:

>>> name = "Fred"
>>> f"He said his name is {name}."
'He said his name is Fred.'

这同样适用于字符串格式化

>>> width=10
>>> string = 'sss'
>>> f'{string: <{width}}'
'sss       '
308

我觉得使用 str.format 这种方式更优雅:

>>> '{0: <5}'.format('s')
's    '
>>> '{0: <5}'.format('ss')
'ss   '
>>> '{0: <5}'.format('sss')
'sss  '
>>> '{0: <5}'.format('ssss')
'ssss '
>>> '{0: <5}'.format('sssss')
'sssss'

如果你想把字符串右对齐,可以用 > 代替 <

>>> '{0: >5}'.format('ss')
'   ss'

编辑 1: 正如评论中提到的,'{0: <5}' 中的 0 表示传给 str.format() 的参数的索引。


编辑 2: 在 Python 3 中,你还可以使用 f-strings:

sub_str='s'
for i in range(1,6):
    s = sub_str*i
    print(f'{s:>5}')
    
'    s'
'   ss'
'  sss'
' ssss'
'sssss'

或者:

for i in range(1,5):
    s = sub_str*i
    print(f'{s:<5}')
's    '
'ss   '
'sss  '
'ssss '
'sssss'

需要注意的是,上面某些地方添加了 ' '(单引号)来强调打印字符串的宽度。

143

编辑于2013-12-11 - 这个回答很旧了。虽然它依然有效且正确,但查看这个内容的人应该更倾向于使用新的格式语法

你可以像这样使用字符串格式化

>>> print '%5s' % 'aa'
   aa
>>> print '%5s' % 'aaa'
  aaa
>>> print '%5s' % 'aaaa'
 aaaa
>>> print '%5s' % 'aaaaa'
aaaaa

基本上:

  • %这个符号告诉Python,它需要把某个东西替换到一个标记里。
  • s这个符号告诉Python,这个标记将会是一个字符串。
  • 5(或者你想要的任何数字)告诉Python,要把字符串用空格填充到5个字符的长度。

在你的具体情况下,一个可能的实现方式可以是:

>>> dict_ = {'a': 1, 'ab': 1, 'abc': 1}
>>> for item in dict_.items():
...     print 'value %3s - num of occurances = %d' % item # %d is the token of integers
... 
value   a - num of occurances = 1
value  ab - num of occurances = 1
value abc - num of occurances = 1

附注:我只是想知道你是否知道有一个itertools模块。比如,你可以用一行代码获得所有组合的列表:

>>> [''.join(perm) for i in range(1, len(s)) for perm in it.permutations(s, i)]
['a', 'b', 'c', 'd', 'ab', 'ac', 'ad', 'ba', 'bc', 'bd', 'ca', 'cb', 'cd', 'da', 'db', 'dc', 'abc', 'abd', 'acb', 'acd', 'adb', 'adc', 'bac', 'bad', 'bca', 'bcd', 'bda', 'bdc', 'cab', 'cad', 'cba', 'cbd', 'cda', 'cdb', 'dab', 'dac', 'dba', 'dbc', 'dca', 'dcb']

而且你可以通过结合使用combinationscount()来获取出现的次数。

撰写回答