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

如何消除Python print输出中$与数字间的多余空格?

解决Python print输出中$和数字间的空格问题

你遇到的问题是因为Python的print()函数在传入多个参数时,默认会用空格分隔参数内容。这里有几种简单的解决方法:

  • 使用f-string(Python 3.6及以上版本推荐):

    print(f'Total: ${total}')
    

    f-string会直接把变量total的值嵌入到字符串中,不会产生额外空格。

  • 字符串拼接:

    print('Total: $' + str(total))
    

    把数字类型的total转成字符串后和前面的文本拼接,这样输出就是连续的内容。

  • 使用str.format()方法:

    print('Total: ${}'.format(total))
    

    用占位符{}接收变量total的值,同样能避免自动添加的空格。

内容的提问来源于stack exchange,提问作者my name jeff

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 14:42:02