反斜杠后如何消除空白(Python 3.4)

时间:2015-08-30 19:43:12

标签: python python-3.x whitespace string-formatting

寻找文本格式的一些建议。我正在尝试打印简单单位转换的结果(英制到公制)但在尝试打印双撇号时会继续获取所有这些空格。

代码:

print("You entered ", imp_height_flt, "\" which converts into " \
    "%.2f" % imp_height_converted, "m.")

当我运行程序时,我得到:

You entered  5.9 " which converts into 1.80 m.

我试图消除9和双撇号之间的空间。

6 个答案:

答案 0 :(得分:3)

对整个字符串使用格式:

print("You entered %.1f\" which converts into "
      "%.2fm." % (imp_height_flt, imp_height_converted))

或者你可以告诉print()函数不要使用空格作为分隔符:

print("You entered ", imp_height_flt, "\" which converts into "
      "%.2f" % imp_height_converted, "m.",
      sep='')

sep参数的默认值为' ',即空格,但您可以将其设置为空字符串。

请注意,根本不需要第一行末尾的反斜杠,因为(..)括号已形成逻辑行。

就个人而言,我在这里使用str.format() method作为字符串模板;它是一种更灵活,更强大的方法,用于将值插入字符串:

print('You entered {:.1f}" which converts into '
      '{:.2f}m.'.format(imp_height_flt, imp_height_converted))

我还使用单个引号来形成字符串文字,这样嵌入的"也不必使用反斜杠。

演示:

>>> imp_height_flt, imp_height_converted = 5.9, 1.8
>>> print("You entered %.1f\" which converts into "
...       "%.2fm." % (imp_height_flt, imp_height_converted))
You entered 5.9" which converts into 1.80m.
>>> print("You entered ", imp_height_flt, "\" which converts into "
...       "%.2f" % imp_height_converted, "m.",
...       sep='')
You entered 5.9" which converts into 1.80m.
>>> print('You entered {:.1f}" which converts into '
...       '{:.2f}m.'.format(imp_height_flt, imp_height_converted))
You entered 5.9" which converts into 1.80m.

答案 1 :(得分:1)

>>> imp_height_flt = 5.9
>>> imp_height_converted = 1.80134334
>>> print('You entered {}" which converts into {:.2f} m.'.format(imp_height_flt, imp_height_converted))
You entered 5.9" which converts into 1.80 m.

答案 2 :(得分:0)

您可以使用'+'运算符:

print("You entered " + str(imp_height_flt) + "\" which converts into " \
    "%.2f" % imp_height_converted, "m.")

答案 3 :(得分:0)

解决这个问题的最简单方法就是做两种字符串格式。

print ("You entered %.2f\" which converts into %.2fm." % (imp_height_flt, imp_height_converted))

答案 4 :(得分:0)

这不是逃避问题。发生这种情况是因为您使用逗号分隔了print的参数,它以块的形式写入stdout并添加空格而不是换行。 像这样:

import sys
args = ("Hello", "world", 123)
for x in args: sys.stdout.write(str(x)+" ")

要避免这种情况,您必须使用一个字符串而不是您使用的所有字符串。 使用“+”运算符将它们添加到一起。之前将int和浮点数转换为str(),一切都会好的:

print("You entered "+str(imp_height_flt)+("\" which converts into %.2f" % imp_height_converted)+" m.")

但更好的解决方案是使用格式化:

print("You entered %.2f\" which converts into %.2f m." % (imp_height_flt, imp_height_converted))

您甚至可以使用单引号来避免转义:

print('You entered %.2f" which converts into %.2f m.' % (imp_height_flt, imp_height_converted))

答案 5 :(得分:0)

其他答案有效(真正的问题是逗号分隔的print args),但我还建议使用format字符串格式化%格式有时会引起问题(最终会消失?): https://docs.python.org/3.4/library/stdtypes.html#old-string-formatting

另外请注意,您可以为格式字符串使用单引号',然后您不必转义双引号"

例如:

height_fmt = 'You entered {}" which converts into {:.2f}m.'
print(height_fmt.format(imp_height_flt, imp_height_converted))

输出是:

You entered 5.9" which converts into 1.80m.

对于format的所有有趣选项,请参阅: https://docs.python.org/3.4/library/string.html#format-specification-mini-language

相关问题