我想在我的代码中添加$,逗号和花括号,当它在python 3.5中打印时
list1 = input('enter four numbers')
一旦我得到了我打印的值
print ('numbers are: ${:,.2f}',format(list1))
一旦打印出来,我就
numbers are: ${:,.2f} 6 7 8 9
答案应该是
{$ 12.95,$ 1,234.56,$ 100.00,$ 20.50}
答案 0 :(得分:2)
您可能希望使用以下内容:
"Numbers are " + ' '.join("${:,.2f}".format(n) for n in list1)
这里基本上有三个操作数:
"Numbers are " +
# Just the simple string tacked on the front
' '.join( ... )
# Joins its contents with a space separator
"${:,.2f}".format(n) for n in list1
# Creates a list of the strings with the requested formatting.
向后展开以构建格式正确的字符串列表,使用空格分隔符将它们连接起来,然后将它们粘贴到静态字符串的末尾。