【问题标题】:Injecting a variable inside a string, in a specific location [duplicate]在特定位置的字符串中注入变量[重复]
【发布时间】:2021-06-20 16:39:27
【问题描述】:

我有这行:

assert response.headers['Content-Disposition'] == 'attachment; filename=myFile.txt'

在该行的第二个字符串中 ('attachment; filename=myFile.txt')

我想删除硬编码的文件名 (myFile.txt) 并将其替换为我拥有的变量。

就像 C 中的 printf(),在某种程度上,用变量替换了那个确切位置的字符串。

【问题讨论】:

    标签: python


    【解决方案1】:

    你可以使用f-string:

    fileName = 'myFile.txt'
    # ...
    assert response.headers['Content-Disposition'] == f'attachment; filename={fileName}'
    

    【讨论】:

      猜你喜欢
      • 2021-09-17
      • 2012-12-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-16
      相关资源
      最近更新 更多