如何打印值之间没有空格的变量
How to print variables without spaces between values
我想知道如何在打印内容时删除多余的空格。
就像我做的那样:
print 'Value is "', value, '"'
输出将是:
Value is " 42 "
但我想要:
Value is "42"
有什么办法吗?
如果您不需要空格,请不要使用 print ...,
(带尾随逗号)。使用字符串连接或格式化。
串联:
print 'Value is "' + str(value) + '"'
格式:
print 'Value is "{}"'.format(value)
后者要灵活得多,参见 str.format()
method documentation and the Formatting String Syntax section。
你还会遇到更老的 %
formatting style:
print 'Value is "%d"' % value
print 'Value is "%d", but math.pi is %.2f' % (value, math.pi)
但这不如较新的 str.format()
方法灵活。
在 Python 3.6 及更新版本中,您将使用格式化字符串 (f-string):
print(f"Value is {value}")
逗号提供了额外的白色 space。
一种方法是使用字符串 %
方法:
print 'Value is "%d"' % (value)
类似于 C 中的 printf
,允许您通过在字符串本身中使用格式说明符来合并和格式化 %
之后的项目。另一个例子,显示了多个值的使用:
print '%s is %3d.%d' % ('pi', 3, 14159)
对于它的价值,Python 3 允许您为单个 print
调用指定分隔符和终止符,从而大大改善了这种情况:
>>> print(1,2,3,4,5)
1 2 3 4 5
>>> print(1,2,3,4,5,end='<<\n')
1 2 3 4 5<<
>>> print(1,2,3,4,5,sep=':',end='<<\n')
1:2:3:4:5<<
>>> value=42
>>> print "Value is %s"%('"'+str(value)+'"')
Value is "42"
以 Martjin 所说的为基础。
我会使用字符串 interpolation/formatting.
在 Python 2.x 中,这似乎是您正在使用的内容,因为您所做的打印功能周围缺少括号:
print 'Value is "%d"' % value
在 Python 3.x 中,您将改用格式方法,因此您的代码将如下所示。
message = 'Value is "{}"'
print(message.format(value))
只是一个面向未来的简单答案,我发现它很容易作为入门者使用:
类似于使用 end=''
来避免换行,您可以使用 sep=''
来避免空格......对于这里的这个问题,它看起来像这样:
print('Value is "', value, '"', sep = '')
希望对以后的人有所帮助。
https://docs.python.org/2/library/functions.html#print
print(*objects, sep=' ', end='\n', file=sys.stdout)
注意:此函数通常不能作为内置函数使用,因为名称 print 被识别为打印语句。要禁用该语句并使用 print() 函数,请在模块顶部使用此 future 语句:
从未来导入print_function
我想知道如何在打印内容时删除多余的空格。
就像我做的那样:
print 'Value is "', value, '"'
输出将是:
Value is " 42 "
但我想要:
Value is "42"
有什么办法吗?
如果您不需要空格,请不要使用 print ...,
(带尾随逗号)。使用字符串连接或格式化。
串联:
print 'Value is "' + str(value) + '"'
格式:
print 'Value is "{}"'.format(value)
后者要灵活得多,参见 str.format()
method documentation and the Formatting String Syntax section。
你还会遇到更老的 %
formatting style:
print 'Value is "%d"' % value
print 'Value is "%d", but math.pi is %.2f' % (value, math.pi)
但这不如较新的 str.format()
方法灵活。
在 Python 3.6 及更新版本中,您将使用格式化字符串 (f-string):
print(f"Value is {value}")
逗号提供了额外的白色 space。
一种方法是使用字符串 %
方法:
print 'Value is "%d"' % (value)
类似于 C 中的 printf
,允许您通过在字符串本身中使用格式说明符来合并和格式化 %
之后的项目。另一个例子,显示了多个值的使用:
print '%s is %3d.%d' % ('pi', 3, 14159)
对于它的价值,Python 3 允许您为单个 print
调用指定分隔符和终止符,从而大大改善了这种情况:
>>> print(1,2,3,4,5)
1 2 3 4 5
>>> print(1,2,3,4,5,end='<<\n')
1 2 3 4 5<<
>>> print(1,2,3,4,5,sep=':',end='<<\n')
1:2:3:4:5<<
>>> value=42
>>> print "Value is %s"%('"'+str(value)+'"')
Value is "42"
以 Martjin 所说的为基础。 我会使用字符串 interpolation/formatting.
在 Python 2.x 中,这似乎是您正在使用的内容,因为您所做的打印功能周围缺少括号:
print 'Value is "%d"' % value
在 Python 3.x 中,您将改用格式方法,因此您的代码将如下所示。
message = 'Value is "{}"'
print(message.format(value))
只是一个面向未来的简单答案,我发现它很容易作为入门者使用:
类似于使用 end=''
来避免换行,您可以使用 sep=''
来避免空格......对于这里的这个问题,它看起来像这样:
print('Value is "', value, '"', sep = '')
希望对以后的人有所帮助。
https://docs.python.org/2/library/functions.html#print
print(*objects, sep=' ', end='\n', file=sys.stdout)
注意:此函数通常不能作为内置函数使用,因为名称 print 被识别为打印语句。要禁用该语句并使用 print() 函数,请在模块顶部使用此 future 语句:
从未来导入print_function