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

如何解决Python中英尺英寸格式输出的空格问题?

解决总英寸转英尺'英寸格式的空格问题

你的问题出在Python的print函数默认会用空格分隔多个传入的参数,所以才会输出带空格的5 ' 9。下面是几种能得到5'9格式的解决方案:

  • 使用f-string(推荐,Python3.6+):
    这是最简洁直观的方式,直接在格式化字符串里拼接内容:

    height = 69  # 示例输入:5英尺9英寸=69总英寸
    height1 = height // 12
    height2 = height % 12
    print(f"{height1}'{height2}")
    
  • 字符串拼接:
    把数值转成字符串后直接连接,避免print的自动空格:

    print(str(height1) + "'" + str(height2))
    
  • str.format()方法:
    适合Python3.6之前的版本:

    print("{}'{}".format(height1, height2))
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 12:55:58