python字符串定长输出_如何将打印输出或字符串格式化为固定宽度?

我有这个代码(在字符串中打印所有排列的出现)

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设置固定长度

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值