在 python 3.5 的輸入列表中添加美元符號、逗號和大括號 (Adding dollar signs, commas and curly brackets to input list in python 3.5)


問題描述

在 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 LawrenceAdam Smith)

參考文件

  1. Adding dollar signs, commas and curly brackets to input list in python 3.5 (CC BY‑SA 2.5/3.0/4.0)

#string #Python #python-3.x






相關問題

VB.net 如何讓流閱讀器忽略某些行? (VB.net how to make stream reader ignore some line?)

Perl Text::CSV_XS 從字符串中讀取 (Perl Text::CSV_XS read from string)

在 D3 中用逗號格式化數字 (Formatting numbers with commas in D3)

我應該使用什麼-String 或 StringBuilder 將 SQL 查詢存儲在使用許多不同 SQL 查詢的代碼中 (what should i use-String or StringBuilder for storing SQL queries in a code which uses many different SQL queries)

在 python 3.5 的輸入列表中添加美元符號、逗號和大括號 (Adding dollar signs, commas and curly brackets to input list in python 3.5)

使用正則表達式處理字符串 (String Manipulation with regular expression)

如何區分數字字符串和字符串? (How do i distinguish between number string and character string?)

如何將單詞的結尾與 Ruby 中的哈希進行比較? (How can I compare the ending of a word with a hash in Ruby?)

在 Python 列表中查找位於特定字符串之間的字符串 (Find Strings Located Between Specific Strings in List Python)

大寫替代字母 (Alternate letters in upper case)

查找 ia strn 列在同一數據框中的列表列中,並創建具有值的第三列 (Find ia a strn column is in a list column in the same data frame and create a 3rd column with a value)

在算術表達式中使用字符串是否安全? (Is it safe to use strings in arithmetic expressions?)







留言討論