简体   繁体   English

在 Python 中打印后如何在两个变量之间添加空格

[英]How do I add space between two variables after a print in Python

I'm fairly new to Python, so I'm trying my hand at some simple code.我对 Python 相当陌生,所以我正在尝试一些简单的代码。 However, in one of the practices my code is supposed to display some numbers in inches on the left and the conversion of the numbers on the right;但是,在其中一种做法中,我的代码应该在左侧显示一些以英寸为单位的数字,并在右侧显示数字的转换;

count = 1
conv = count * 2.54
print count, conv

I want the output to be printed with some space between them;我希望输出打印出来,它们之间有一些空间;

count = 1
conv = count * 2.54
print count,     conv

I can't figure out how to do this.我无法弄清楚如何做到这一点。 I've searched everywhere, but all I can find are people trying to get rid of space.我到处搜索,但我能找到的只是试图摆脱空间的人。 If someone could just lead me in the right direction, I'd be thankful.如果有人能将我引向正确的方向,我将不胜感激。

Oh, and I just realized that I'm using Python 2.7, not 3.x .哦,我刚刚意识到我使用的是 Python 2.7,而不是 3.x Not sure if this is important.不确定这是否重要。

A simple way would be:一个简单的方法是:

print str(count) + '  ' + str(conv)

If you need more spaces, simply add them to the string:如果您需要更多空格,只需将它们添加到字符串中:

print str(count) + '    ' + str(conv)

A fancier way, using the new syntax for string formatting:一种更高级的方式,使用新的字符串格式化语法:

print '{0}  {1}'.format(count, conv)

Or using the old syntax, limiting the number of decimals to two:或者使用旧语法,将小数位数限制为两位:

print '%d  %.2f' % (count, conv)

改用字符串插值

print '%d   %f' % (count,conv)

Alternatively you can use ljust/rjust to make the formatting nicer.或者,您可以使用ljust/rjust使格式更好。

print "%s%s" % (str(count).rjust(10), conv)

or或者

print str(count).ljust(10), conv

You can do it this way in python3:你可以在python3中这样做:

print(a,b,end=" ")打印(a,b,end="")

A quick warning, this a pretty wordy answer.快速警告,这是一个非常冗长的答案。

print is tricky sometimes, I had some problems with it when I first started.打印有时很棘手,我刚开始时遇到了一些问题。 What you want is a few spaces in between two variables after you print them right?你想要的是两个变量之间的几个空格打印出来后对吗? There's many ways to do this, as shown in the above answers.有很多方法可以做到这一点,如上面的答案所示。

This is your code:这是你的代码:

count = 1
conv = count * 2.54
print count, conv

It's output is this:它的输出是这样的:

1 2.54

If you want spaces in between, you can do it the naive way by sticking a string of spaces in between them.如果你想要中间有空格,你可以通过在它们之间粘贴一串空格来实现。 The variables count and conv need to be converted to string types to concatenate(join) them together.变量 count 和 conv 需要转换为字符串类型以将它们连接在一起。 This is done with str().这是通过 str() 完成的。

print (str(count) + "           " + str(conv))
### Provides an output of:
1           2.54

To do this is the newer, more pythonic way, we use the % sign in conjunction with a letter to denote the kind of value we're using.要做到这一点,这是更新的、更 Python 化的方式,我们使用 % 符号和一个字母来表示我们正在使用的值类型。 Here I use underscores instead of spaces to show how many there are.在这里,我使用下划线而不是空格来显示有多少。 The modulo before the last values just tells python to insert the following values in, in the order we provided.最后一个值之前的模只是告诉python按照我们提供的顺序插入以下值。

print ('%i____%s' % (count, conv))
### provides an output of:
1____2.54

I used %i for count because it is a whole number, and %s for conv, because using %i in that instance would provide us with "2" instead of "2.54" Technically, I could've used both %s, but it's all good.我使用 %i 表示计数,因为它是一个整数,而 %s 表示转换,因为在这种情况下使用 %i 将为我们提供“2”而不是“2.54”技术上,我可以同时使用 %s,但是都很好。

I hope this helps!我希望这有帮助!

-Joseph -约瑟夫

PS if you want to get complicated with your formatting, you should look at prettyprint for large amounts of text such as dictionaries and tuple lists(imported as pprint) as well as which does automatic tabs, spacing and other cool junk. PS,如果你想让你的格式变得复杂,你应该查看大量文本的prettyprint,例如字典和元组列表(作为pprint导入)以及自动制表符,间距和其他很酷的垃圾。

Here's some more information about strings in the python docs.以下是 Python 文档中有关字符串的更多信息。 http://docs.python.org/library/string.html#module-string http://docs.python.org/library/string.html#module-string

If you are trying only to print and not to store variables.如果您只想打印而不是存储变量。 You can use sep= parameter in print() function, as below:您可以在print()函数中使用sep=参数,如下所示:

print("string", "string", sep="something between them")
space_num = 10

count = 1
conv = count * 2.54
print(count, conv, sep=" "*space_num)

If you want to store values as one variable, so I think it will be best to use f string, as below:如果要将值存储为一个变量,那么我认为最好使用f字符串,如下所示:

space_num = 10

count = 1
conv = count * 2.54

result = f"{count}{' ' * space_num}{conv}"
print(result)

This is a stupid/hacky way这是一种愚蠢/笨拙的方式

print count,    
print conv

print str(count) + ' ' + str(conv) - This did not work. print str(count) + ' ' + str(conv) - 这不起作用。 However, replacing + with , works for me但是,将+替换为,对我有用

You should use python Explicit Conversion Flag PEP-3101你应该使用 python 显式转换标志 PEP-3101

'My name is {0!s:10} {1}'.format('Dunkin', 'Donuts')

'My name is Dunkin Donuts'

or或者

'My name is %-10s %s' % ('Dunkin', 'Donuts')

'My name is Dunkin Donuts'

https://www.python.org/dev/peps/pep-3101/ https://www.python.org/dev/peps/pep-3101/

添加选项卡的一种简单方法是使用\\t标签。

print '{0} \t {1}'.format(count, conv)
print( "hello " +k+ "  " +ln);

其中kln是变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM