括号内的Python格式

时间:2014-09-20 21:43:07

标签: python string python-2.7 formatting

是否可以格式化括号内?我的意思是:

print "Formatting: { 0 : {1} s }".format("""\
long piece of text that I need to format, but I can't explicitly say\
how long it is going to be because it is coming from another function\
that gives me the length of how many characters I can print. So, it would be\
really helpful if someone could help me with this""" , charMax())

import random

def charMax():
    return random.randint(10, 80)

有人可以帮我确定如何模拟上面的sudo代码吗?

3 个答案:

答案 0 :(得分:1)

您的格式说明符错误。字符串长度限制由精度

决定
"{0:.{1}s}".format(...)

答案 1 :(得分:0)

最好将长度放在,并使用.length 精度格式来限制长度:

"Formatting: {1:.{0}s}".format(charMax(), """Some long string""")

现在,字符串的格式为最大长度,而不是最小值:

>>> some_long_string = """\
... long piece of text that I need to format, but I can't explicitly say\
... how long it is going to be because it is coming from another function\
... that gives me the length of how many characters I can print. So, it would be\
... really helpful if someone could help me with this"""
>>> print "Formatting: {1:.{0}s}".format(charMax(), some_long_string)
Formatting: long piece of text that I need to format, bu
>>> print "Formatting: {1:.{0}s}".format(charMax(), some_long_string)
Formatting: long piece of text that I need to format, but I can't explicitly 
>>> print "Formatting: {1:.{0}s}".format(charMax(), some_long_string)
Formatting: long piece of text that I 

它也可以与订单相反:

>>> print "Formatting: {0:.{1}s}".format(some_long_string, charMax())
Formatting: long piece of text that I need to format, but I can't ex

但更清楚的是让读者看到相反的情况。

答案 2 :(得分:0)

如果你想要一个所需长度的字符串,你很可能需要一个函数,format对你来说不会有太大作用:

def charMax():
    return random.randint(10, 80)

def stringLength(originalStr, length):
    return originalStr[:length]

然后然后你可以进行格式化:

print "Formatting: {0}".format(stringLength("long piece of text that I need to format, but I can't explicitly say how long it is going to be because it is coming from another function that gives me the length of how many characters I can print. So, it would be really helpful if someone could help me with this", charMax()))

>>> print "Formatting: {0}".format(stringLength("long piece of text that I need to format, but I can't explicitly say how long it is going to be because it is coming from another function that gives me the length of how many characters I can print. So, it would be really helpful if someone could help me with this", charMax()))
Formatting: long piece of text that I need to format, 
>>> print "Formatting: {0}".format(stringLength("long piece of text that I need to format, but I can't explicitly say how long it is going to be because it is coming from another function that gives me the length of how many characters I can print. So, it would be really helpful if someone could help me with this", charMax()))
Formatting: long piece of text that I n
>>> print "Formatting: {0}".format(stringLength("long piece of text that I need to format, but I can't explicitly say how long it is going to be because it is coming from another function that gives me the length of how many characters I can print. So, it would be really helpful if someone could help me with this", charMax()))
Formatting: long piece of text that I need to format, but I can't expl
>>> print "Formatting: {0}".format(stringLength("long piece of text that I need to format, but I can't explicitly say how long it is going to be because it is coming from another function that gives me the length of how many characters I can print. So, it would be really helpful if someone could help me with this", charMax()))
Formatting: long piece of text that I nee
>>>