是否有格式代码将 -2.34 格式化为“-2.3”,但将 +2.34 格式化为“2.3”(注意前导空格)?基本上显示负号,但为正号留出空间。
最佳答案
使用“”(空格)在正数前插入一个空格,在负数前插入一个减号:
txt = "The temperature is between {: } and {: } degrees celsius."
print(txt.format(-3, 7))
答案:
The temperature is between -3 and 7 degrees celsius.
关于python字符串格式,负号表示负数,但空格表示正数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62212417/