You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python格式化字符串报错TypeError: not enough arguments for format string求解决

问题分析与解决
  • 错误根源:%格式化语法中,当右侧是列表这类序列时,直接使用format_string % data会把整个列表视为单个参数,但你的格式化字符串有3个%s占位符,需要3个独立参数,参数数量不匹配导致TypeError。
  • 两种修正方式:
    1. 用*解包列表,将列表元素逐个传入:
      data = ["John", "poor fella", 100]
      format_string = "%s is a %s, his bank account has $%s."
      print(format_string % (*data,))
      
    2. 将列表改为元组(元组可直接被%正确识别为多参数):
      data = ("John", "poor fella", 100)
      format_string = "%s is a %s, his bank account has $%s."
      print(format_string % data)
      
  • 注意:要得到目标输出「John is a poor fella, his bank account has $100.」,需把原代码中的Zac替换为John。

内容的提问来源于stack exchange,提问作者Zack

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 10:52:04