[英]generate a list of string elements without the quotation mark
我想生成一個字符串元素列表,但沒有引號。 這就是我生成列表的方式:
[f'test_{i+1}' for i in range(5)]
這會產生以下結果:
['test_1', 'test_2', 'test_3', 'test_4', 'test_5']
如何刪除配額標記? 我嘗試如下所示,但這給了我一個語法錯誤。
[f'test_{i+1}' for i in range(5)].replace(''', '')
如果由於某種原因您不能在打印語句中使用引號,您可以使用
print(', '.join(['test_1', 'test_2', 'test_3', 'test_4', 'test_5']))
請注意,這是將所有元素連接到一個字符串中。
您的字符串中沒有引號。 您嘗試刪除的引號是 Python 語法的一部分。 它們是分隔字符串所必需的。 您無法刪除它們。
PS:Python 列表沒有replace
方法。 如果要替換字符串中的任何內容,可以使用以下語法:
a = # the character to be replaced
b = # the character to replace a
[f'test_{i+1}'.replace(a, b) for i in range(5)]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.