【问题标题】:How do I insert something into multiple strings?如何在多个字符串中插入一些东西?
【发布时间】:2020-12-11 16:13:46
【问题描述】:

我有 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'

【问题讨论】:

    标签: python string numpy jupyter


    【解决方案1】:
    inserted = [month.format(insert) for month in month_names]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-09
      • 2020-02-23
      相关资源
      最近更新 更多