如何打印值之间没有空格的变量[重复]

时间:2022-06-20 21:45:07

This question already has an answer here:

这个问题在这里已有答案:

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?

有没有办法做到这一点?

6 个解决方案

#1


34  

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 documentation and the Formatting String Syntax section.

后者更灵活,请参阅str.format()方法文档和Formatting String Syntax部分。

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()方法那样灵活。

#2


12  

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.

可以帮助将来的某个人。

#3


3  

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 printf in 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:

这与C中的printf类似,允许您通过在字符串本身中使用格式说明符来合并和格式化%之后的项目。另一个例子,显示了多个值的使用:

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 print call:

对于它的价值,Python 3允许您为单个打印调用指定分隔符和终止符,从而大大改善了这种情况:

>>> 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<<

#4


1  

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))

#5


1  

https://docs.python.org/2/library/functions.html#print

print(*objects, sep=' ', end='\n', file=sys.stdout)

print(* objects,sep ='',end ='\ n',file = 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 future import print_function

来自未来的import print_function

#6


-1  

>>> value=42

>>> print "Value is %s"%('"'+str(value)+'"') 

Value is "42"

#1


34  

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 documentation and the Formatting String Syntax section.

后者更灵活,请参阅str.format()方法文档和Formatting String Syntax部分。

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()方法那样灵活。

#2


12  

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.

可以帮助将来的某个人。

#3


3  

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 printf in 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:

这与C中的printf类似,允许您通过在字符串本身中使用格式说明符来合并和格式化%之后的项目。另一个例子,显示了多个值的使用:

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 print call:

对于它的价值,Python 3允许您为单个打印调用指定分隔符和终止符,从而大大改善了这种情况:

>>> 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<<

#4


1  

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))

#5


1  

https://docs.python.org/2/library/functions.html#print

print(*objects, sep=' ', end='\n', file=sys.stdout)

print(* objects,sep ='',end ='\ n',file = 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 future import print_function

来自未来的import print_function

#6


-1  

>>> value=42

>>> print "Value is %s"%('"'+str(value)+'"') 

Value is "42"