小编NIS*_*ARU的帖子

星号`*`如何在Python 3中的字符串格式化方法`.format(*)`中工作?

什么是使用*.format(*)?在下面的格式函数中使用它时,print(new_string.format(*sum_string))它会将输出中sum_string的值从18更改为1为什么会发生这种情况?我已阅读以下链接*args,**kwargs但无法理解这是如何适用于该.format()功能

**(双星/星号)和*(星号/星号)对参数有什么作用?

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
Run Code Online (Sandbox Code Playgroud)

python string.format string-formatting args python-3.x

2
推荐指数
1
解决办法
1029
查看次数