Python 如何打印值之间没有空格的变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28669459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to print variables without spaces between values
提问by nookonee
I would like to know how to remove additional spaces when I print something.
我想知道如何在打印某些内容时删除额外的空格。
Like when I do:
就像我做的那样:
print 'Value is "', value, '"'
The output will be:
输出将是:
Value is " 42 "
But I want:
但我想要:
Value is "42"
Is there any way to do this?
有没有办法做到这一点?
采纳答案by Martijn Pieters
Don't use print ...,if you don't want spaces. Use string concatenation or formatting.
print ...,如果您不想要空格,请不要使用。使用字符串连接或格式。
Concatenation:
级联:
print 'Value is "' + str(value) + '"'
Formatting:
格式化:
print 'Value is "{}"'.format(value)
The latter is far more flexible, see the str.format()method documentationand the Formatting String Syntaxsection.
后者更加灵活,请参阅str.format()方法文档和格式化字符串语法部分。
You'll also come across the older %formatting style:
您还会遇到旧的%格式样式:
print 'Value is "%d"' % value
print 'Value is "%d", but math.pi is %.2f' % (value, math.pi)
but this isn't as flexible as the newer str.format()method.
但这不如较新的str.format()方法灵活。
回答by paxdiablo
It's the comma which is providing that extra white space.
逗号提供了额外的空白。
One way is to use the string %method:
一种方法是使用字符串%方法:
print 'Value is "%d"' % (value)
which is like printfin C, allowing you to incorporate and format the items after %by using format specifiers in the string itself. Another example, showing the use of multiple values:
这就像printf在 C 中一样,允许您%通过在字符串本身中使用格式说明符来合并和格式化项目。另一个示例,显示了多个值的使用:
print '%s is %3d.%d' % ('pi', 3, 14159)
For what it's worth, Python 3 greatly improves the situation by allowing you to specify the separator and terminator for a single printcall:
就其价值而言,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<<
回答by user3694147
>>> value=42
>>> print "Value is %s"%('"'+str(value)+'"')
Value is "42"
回答by GallegoDor
To build off what Martjin was saying. I'd use string interpolation/formatting.
建立在Martjin所说的基础上。我会使用字符串插值/格式。
In Python 2.x which seems to be what you're using due to the lack of parenthesis around the print function you do:
在 Python 2.x 中,由于在您执行的打印函数周围缺少括号,这似乎是您正在使用的:
print 'Value is "%d"' % value
In Python 3.x you'd use the format method instead, so you're code would look like this.
在 Python 3.x 中,您将改用 format 方法,因此您的代码将如下所示。
message = 'Value is "{}"'
print(message.format(value))
回答by Flo
Just an easy answer for the future which I found easy to use as a starter:
Similar to using end=''to avoid a new line, you can use sep=''to avoid the white spaces...for this question here, it would look like this:
print('Value is "', value, '"', sep = '')
只是一个简单的未来答案,我发现它很容易用作初学者:类似于用于end=''避免换行,您可以使用sep=''来避免空格......对于这里的这个问题,它看起来像这样:
print('Value is "', value, '"', sep = '')
May it help someone in the future.
愿它在未来帮助某人。
回答by Mauro
https://docs.python.org/2/library/functions.html#print
https://docs.python.org/2/library/functions.html#print
print(*objects, sep=' ', end='\n', file=sys.stdout)
打印(*对象,sep='',end='\n',文件=sys.stdout)
Note: This function is not normally available as a built-in since the name print is recognized as the print statement. To disable the statement and use the print() function, use this future statement at the top of your module:
注意:此函数通常不能作为内置函数使用,因为名称 print 被识别为 print 语句。要禁用该语句并使用 print() 函数,请在模块顶部使用此 future 语句:
from futureimport print_function
从未来导入 print_function

