【发布时间】:2018-09-24 08:29:27
【问题描述】:
.format(*) 中的* 有什么用?
在print(new_string.format(*sum_string)) 下面的格式函数中使用它时,它会将输出中 sum_string 的值从 18 更改为 1
为什么会这样?
我已阅读以下有关 *args 和 **kwargs 的链接,但不明白它如何应用于 .format() 函数
What does ** (double star/asterisk) and * (star/asterisk) do for parameters?
sum_string = "18"
new_string = "This is a new string of value {}"
print(new_string.format(sum_string)) #it provides an output of value 18
print(new_string.format(*sum_string)) #it provides an output of value 1
【问题讨论】:
-
我不同意这个骗局。 OP 引用了一个类似的问题,但不明白格式如何“解释”这一点。
标签: python python-3.x string-formatting args string.format