如何消除反斜杠后的空白(Python 3.4)
寻找一些关于文本格式的建议。我试图打印一个简单的单位转换(英制到公制)的结果,但在打印双撇号符号时,会一直获得所有这些空格 代码: 当我运行程序时,我得到:如何消除反斜杠后的空白(Python 3.4),python,python-3.x,whitespace,string-formatting,Python,Python 3.x,Whitespace,String Formatting,寻找一些关于文本格式的建议。我试图打印一个简单的单位转换(英制到公制)的结果,但在打印双撇号符号时,会一直获得所有这些空格 代码: 当我运行程序时,我得到: You entered 5.9 " which converts into 1.80 m. 我试图消除9和双撇号之间的空格。对整个字符串使用格式: print("You entered %.1f\" which converts into " "%.2fm." % (imp_height_flt, imp_height_co
You entered 5.9 " which converts into 1.80 m.
我试图消除9和双撇号之间的空格。对整个字符串使用格式:
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
参数的默认值是'
,一个空格,但您可以将其设置为空字符串
请注意,第一行末尾的反斜杠根本不需要,因为(..)
括号已经构成了逻辑行
就我个人而言,我会在这里使用字符串模板;这是一种更灵活、更强大的方法,用于将值插值到字符串中:
print('You entered {:.1f}" which converts into '
'{:.2f}m.'.format(imp_height_flt, imp_height_converted))
我还使用单引号来形成字符串文字,这样嵌入的“
也不必使用反斜杠
演示:
对整个字符串使用格式:
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
参数的默认值是'
,一个空格,但您可以将其设置为空字符串
请注意,第一行末尾的反斜杠根本不需要,因为(..)
括号已经构成了逻辑行
就我个人而言,我会在这里使用字符串模板;它是一种更灵活、更强大的方法,用于将值插值到字符串中:
print('You entered {:.1f}" which converts into '
'{:.2f}m.'.format(imp_height_flt, imp_height_converted))
我还使用单引号来形成字符串文字,这样嵌入的“
也不必使用反斜杠
演示:
您可以使用“+”运算符:
print("You entered " + str(imp_height_flt) + "\" which converts into " \
"%.2f" % imp_height_converted, "m.")
您可以使用“+”运算符:
print("You entered " + str(imp_height_flt) + "\" which converts into " \
"%.2f" % imp_height_converted, "m.")
解决这个问题最简单的方法就是使用两种字符串格式
print ("You entered %.2f\" which converts into %.2fm." % (imp_height_flt, imp_height_converted))
解决这个问题最简单的方法就是使用两种字符串格式
print ("You entered %.2f\" which converts into %.2fm." % (imp_height_flt, imp_height_converted))
这不是逃避问题。之所以会出现这种情况,是因为您用逗号分隔了print的参数,逗号以块的形式写入标准输出,并附加空格而不是新行。 像这样:
import sys
args = ("Hello", "world", 123)
for x in args: sys.stdout.write(str(x)+" ")
为了避免这种情况,您必须使用一个字符串,而不是使用所有字符串。
使用“+”运算符将它们相加。将int和float转换为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))
这不是逃避问题。之所以会出现这种情况,是因为您用逗号分隔了print的参数,逗号以块的形式写入标准输出,并附加空格而不是新行。 像这样:
import sys
args = ("Hello", "world", 123)
for x in args: sys.stdout.write(str(x)+" ")
为了避免这种情况,您必须使用一个字符串,而不是使用所有字符串。
使用“+”运算符将它们相加。将int和float转换为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))
其他答案是有效的(真正的问题是用逗号分隔的参数来打印
),但我也建议使用格式
,因为%
字符串格式有时会导致问题(最终会消失?):
另一个注意事项是,您可以对格式字符串使用单引号”
,然后不必转义双引号“
例如:
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.
有关格式的所有有趣选项,请参见:
其他答案也有效(真正的问题是打印的逗号分隔参数),但我也建议使用格式设置格式,因为%
字符串格式有时会导致问题(最终会消失?):
另一个注意事项是,您可以对格式字符串使用单引号”
,然后不必转义双引号“
例如:
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.
有关格式的所有有趣选项,请参见:
这与反斜杠无关。您应该查看。请参见:这与反斜杠无关。您应该查看。另见此: