如何将内容插入多个字符串?
How do I insert something into multiple strings?
我有 12 个不同的字符串需要元组格式,因为稍后我将在图表中使用这些字符串。
如何将相同的字符串添加到字符串数组中?
我有这几个月:
一月、二月等,我想在每个字符串中插入“一月 LSDS”、“二月 LSDS”等
我试过了,但出现错误:
insert = 'LSDS'
month_names = ('January {}', 'February {}','March {}','April {}', 'May {}', 'June {}', 'July {}', 'August {}', 'September {}', 'October {}', 'November {}', 'December {}').format(insert)
print(month_names)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-167-c79c038f3ebc> in <module>
3 insert = 'LSDS'
4
----> 5 month_names = ('January {}', 'February {}','March {}','April {}', 'May {}', 'June {}', 'July {}', 'August {}', 'September {}', 'October {}', 'November {}', 'December {}').format(insert)
6
7 print(month_names)
AttributeError: 'tuple' object has no attribute 'format'
inserted = [month.format(insert) for month in month_names]
我有 12 个不同的字符串需要元组格式,因为稍后我将在图表中使用这些字符串。 如何将相同的字符串添加到字符串数组中?
我有这几个月: 一月、二月等,我想在每个字符串中插入“一月 LSDS”、“二月 LSDS”等
我试过了,但出现错误:
insert = 'LSDS'
month_names = ('January {}', 'February {}','March {}','April {}', 'May {}', 'June {}', 'July {}', 'August {}', 'September {}', 'October {}', 'November {}', 'December {}').format(insert)
print(month_names)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-167-c79c038f3ebc> in <module>
3 insert = 'LSDS'
4
----> 5 month_names = ('January {}', 'February {}','March {}','April {}', 'May {}', 'June {}', 'July {}', 'August {}', 'September {}', 'October {}', 'November {}', 'December {}').format(insert)
6
7 print(month_names)
AttributeError: 'tuple' object has no attribute 'format'
inserted = [month.format(insert) for month in month_names]