typeerror:格式字符串PYTHON的参数不足

typeerror:格式字符串PYTHON的参数不足,python,string,Python,String,我正在学习Python2.7.8。我不知道出了什么问题,我已经尝试了所有发布的答案。代码如下: formatterb = "%r %r %r %r" print formatterb % ( "I am tired", "Not so tired tho" "this had better work.", "been at this for a while") 下面是我收到的错误消息: TypeError: not enough arguments for

我正在学习Python2.7.8。我不知道出了什么问题,我已经尝试了所有发布的答案。代码如下:

formatterb = "%r %r %r %r"
print formatterb % (
    "I am tired", 
    "Not so tired tho"
    "this had better work.", 
    "been at this for a while")
下面是我收到的错误消息:

TypeError: not enough arguments for format strings.

您在
“不太累”和“这最好能工作”之间缺少一个逗号,
,因此没有足够的参数

应该是:

formatterb = "%r %r %r %r"
print formatterb % (
    "I am tired", 
    "Not so tired tho",
    "this had better work.", 
    "been at this for a while")
根据OP的评论进行编辑:

字符串中不能有4个格式说明符,但伴随的元组中只能有3个元素。如果您希望第二个和第三个参数在同一行上,那么它们应该是相同的参数,即
“不太累,但这样做更好”

如果您想在单独的行中添加项目,通常可以添加换行,例如:

formatterb = "%r %r %r"
print formatterb % (
    "I am tired", 
    "Not so tired tho\nthis had better work.", 
    "been at this for a while")
\n
将在两个子字符串之间添加一个换行符。在这两种情况下,您都希望删除其中一个格式说明符

但是,还要注意,
%r
相当于
repr
,因此您将看到实际的字符串内容,而不是打印出来的内容。e、 g.即使使用换行符,您也会看到
\n
,而不是实际的换行符

如果将
%r
替换为
%s
,它将显示如何打印,您将看到它的输出如下:

I am tired Not so tired tho
this had better work. been at this for a while

您在
“不太累”和“这最好能工作”之间缺少一个逗号,
,因此没有足够的参数

应该是:

formatterb = "%r %r %r %r"
print formatterb % (
    "I am tired", 
    "Not so tired tho",
    "this had better work.", 
    "been at this for a while")
根据OP的评论进行编辑:

字符串中不能有4个格式说明符,但伴随的元组中只能有3个元素。如果您希望第二个和第三个参数在同一行上,那么它们应该是相同的参数,即
“不太累,但这样做更好”

如果您想在单独的行中添加项目,通常可以添加换行,例如:

formatterb = "%r %r %r"
print formatterb % (
    "I am tired", 
    "Not so tired tho\nthis had better work.", 
    "been at this for a while")
\n
将在两个子字符串之间添加一个换行符。在这两种情况下,您都希望删除其中一个格式说明符

但是,还要注意,
%r
相当于
repr
,因此您将看到实际的字符串内容,而不是打印出来的内容。e、 g.即使使用换行符,您也会看到
\n
,而不是实际的换行符

如果将
%r
替换为
%s
,它将显示如何打印,您将看到它的输出如下:

I am tired Not so tired tho
this had better work. been at this for a while

这样使用时,模的名称是什么?是的,那是因为我希望它们在单独的行上,而不是在一行上@khampson@kpie-不知道你在问什么。您指的是
%r
?当然,没问题。请接受这个答案,因为它似乎已经回答了你的问题。当像这样使用时,模的名称是什么?是的,那是因为我希望它们在单独的行上,而不是在一行上@khampson@kpie-不知道你在问什么。您指的是
%r
?当然,没问题。请接受答案,因为它似乎已经回答了您的问题。