Python: 用'.format'格式化的字符串可以乘以'*'吗?
Python: Can string formatted with '.format' be multiplied with '*'?
questions = [
"We don't serve strings around here. Are you a string?",
"What is said on Father's Day in the forest?",
"What makes the sound 'Sis! Boom! Bah!'?"
]
answers = [
"An exploding sheep.",
"No, I'm a frayed knot.",
"'Pop!' goes the wessel."
]
print('Q: {}\nA: {}\n'.format(questions[0], answers[0]))
print('Q: {}\nA: {}\n'.format(questions[1], answers[1]))
print('Q: {}\nA: {}\n'.format(questions[2], answers[2]))
print('Q: {}\nA: {}\n' * 3(questions[0], answers[0], questions[1], answers[1], questions[2], answers[2]))
这两种逐个打印问题和答案的方式给出了相同的输出。
但是当我尝试时
print('Q: {}\nA: {}\n'.format *3 (questions[0], answers[0], questions[1], answers[1], questions[2], answers[2]))
发生异常并显示“'int' 对象不可调用”。
为什么不起作用?仅仅是因为'*'不能与'.format'一起使用吗?
所以当我尝试你的第二种方法时:
print('Q: {}\nA: {}\n' * 3(questions[0], answers[0], questions[1], answers[1], questions[2], answers[2]))
我得到了与您指出的错误相同的 TypeError: 'int' object is not callable
错误。
原因
在每种情况下,python 正在读取 3(questions[0], ...)
作为对 int
对象 3
的调用,这是失败的。
解决方案
但是,如果您重新排列乘法,将字符串乘以 3,然后将 .format()
应用于生成的字符串,您将获得所需的结果:
print(('Q: {}\nA: {}\n'*3).format(questions[0], answers[0], questions[1], answers[1], questions[2], answers[2]))
产出
>>> Q: We don't serve strings around here. Are you a string?
>>> A: An exploding sheep.
>>> Q: What is said on Father's Day in the forest?
>>> A: No, I'm a frayed knot.
>>> Q: What makes the sound 'Sis! Boom! Bah!'?
>>> A: 'Pop!' goes the wessel.
P.S。旧样式格式
%
样式格式可以类似地使用。 *
和 %
具有相同的运算符优先级,因此首先将字符串相乘,然后进行 %
样式格式化。
print("Q: %s\nA: %s\n"*3%(questions[0], answers[0], questions[1], answers[1], questions[2], answers[2]))
与之前相同的输出。
questions = [
"We don't serve strings around here. Are you a string?",
"What is said on Father's Day in the forest?",
"What makes the sound 'Sis! Boom! Bah!'?"
]
answers = [
"An exploding sheep.",
"No, I'm a frayed knot.",
"'Pop!' goes the wessel."
]
print('Q: {}\nA: {}\n'.format(questions[0], answers[0]))
print('Q: {}\nA: {}\n'.format(questions[1], answers[1]))
print('Q: {}\nA: {}\n'.format(questions[2], answers[2]))
print('Q: {}\nA: {}\n' * 3(questions[0], answers[0], questions[1], answers[1], questions[2], answers[2]))
这两种逐个打印问题和答案的方式给出了相同的输出。
但是当我尝试时
print('Q: {}\nA: {}\n'.format *3 (questions[0], answers[0], questions[1], answers[1], questions[2], answers[2]))
发生异常并显示“'int' 对象不可调用”。
为什么不起作用?仅仅是因为'*'不能与'.format'一起使用吗?
所以当我尝试你的第二种方法时:
print('Q: {}\nA: {}\n' * 3(questions[0], answers[0], questions[1], answers[1], questions[2], answers[2]))
我得到了与您指出的错误相同的 TypeError: 'int' object is not callable
错误。
原因
在每种情况下,python 正在读取 3(questions[0], ...)
作为对 int
对象 3
的调用,这是失败的。
解决方案
但是,如果您重新排列乘法,将字符串乘以 3,然后将 .format()
应用于生成的字符串,您将获得所需的结果:
print(('Q: {}\nA: {}\n'*3).format(questions[0], answers[0], questions[1], answers[1], questions[2], answers[2]))
产出
>>> Q: We don't serve strings around here. Are you a string?
>>> A: An exploding sheep.
>>> Q: What is said on Father's Day in the forest?
>>> A: No, I'm a frayed knot.
>>> Q: What makes the sound 'Sis! Boom! Bah!'?
>>> A: 'Pop!' goes the wessel.
P.S。旧样式格式
%
样式格式可以类似地使用。 *
和 %
具有相同的运算符优先级,因此首先将字符串相乘,然后进行 %
样式格式化。
print("Q: %s\nA: %s\n"*3%(questions[0], answers[0], questions[1], answers[1], questions[2], answers[2]))
与之前相同的输出。