問題描述
在 python 3.5 的輸入列表中添加美元符號、逗號和大括號 (Adding dollar signs, commas and curly brackets to input list in python 3.5)
當我在 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}
參考解法
方法 1:
You might want to use something like:
"Numbers are " + ' '.join("${:,.2f}".format(n) for n in list1)
There are basically three operands here:
"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.
Unroll that backwards to build a list of the properly formatted strings, join them with a space separator, and tack them on to the end of your static string.
(by West Lawrence、Adam Smith)