在 Python 3.4 中打印变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26938799/
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
Printing variables in Python 3.4
提问by algorhythm
So the syntax seems to have changed from what I learned in Python 2... here is what I have so far
所以语法似乎与我在 Python 2 中学到的有所不同......这是我到目前为止所拥有的
for key in word:
i = 1
if i < 6:
print ( "%s. %s appears %s times.") % (str(i), key, str(wordBank[key]))
The first value being an int, the second a string, and the final an int.
第一个值是 int,第二个是字符串,最后一个是 int。
How can I alter my print statement so that it prints the the variables correctly?
如何更改我的打印语句以正确打印变量?
采纳答案by adarsh
The syntax has changed in that printis now a function. This means that the %formatting needs to be done inside the parenthesis:1
语法已经改变,print现在是一个函数。这意味着%需要在括号内完成格式化:1
print("%d. %s appears %d times." % (i, key, wordBank[key]))
However, since you are using Python 3.x., you should actually be using the newer str.formatmethod:
但是,由于您使用的是 Python 3.x.,您实际上应该使用较新的str.format方法:
print("{}. {} appears {} times.".format(i, key, wordBank[key]))
Though %formatting is not officiallydeprecated (yet), it is discouraged in favor of str.formatand will most likely be removed from the language in a coming version (Python 4 maybe?).
尽管%格式化尚未正式弃用(还),但不鼓励使用,str.format并且很可能会在即将到来的版本中从语言中删除(也许是 Python 4?)。
1Just a minor note: %dis the format specifier for integers, not %s.
1只是一个小注意:%d是整数的格式说明符,而不是%s.
回答by adarsh
The problem seems to be a mis-placed ). In your sample you have the %outside of the print(), you should move it inside:
问题似乎是放错了). 在您的示例中,您有 的%外部print(),您应该将其移动到内部:
Use this:
用这个:
print("%s. %s appears %s times." % (str(i), key, str(wordBank[key])))
回答by horns
Try the format syntax:
尝试格式语法:
print ("{0}. {1} appears {2} times.".format(1, 'b', 3.1415))
Outputs:
输出:
1. b appears 3.1415 times.
The print function is called just like any other function, with parenthesis around all its arguments.
打印函数就像任何其他函数一样被调用,它的所有参数都用括号括起来。
回答by Erion S
You can also format the string like so:
您还可以像这样格式化字符串:
>>> print ("{index}. {word} appears {count} times".format(index=1, word='Hello', count=42))
Which outputs
哪些输出
1. Hello appears 42 times.
Because the values are named, their order does not matter. Making the example below output the same as the above example.
因为值是命名的,所以它们的顺序无关紧要。使下面的示例输出与上面的示例相同。
>>> print ("{index}. {word} appears {count} times".format(count=42, index=1, word='Hello'))
Formatting string this way allows you to do this.
以这种方式格式化字符串允许您执行此操作。
>>> data = {'count':42, 'index':1, 'word':'Hello'}
>>> print ("{index}. {word} appears {count} times.".format(**data))
1. Hello appears 42 times.
回答by wwii
Version 3.6+: Use a formatted string literal, f-stringfor short
版本 3.6+:使用格式化字符串文字,简称f-string
print(f"{i}. {key} appears {wordBank[key]} times.")
回答by Rashi Goyal
one can print values using the format method in python. This small example will help take input of two numbers a and b. Print a+b in first line and a-b in second line
可以使用 python 中的 format 方法打印值。这个小例子将有助于输入两个数字 a 和 b。在第一行打印 a+b,在第二行打印 ab
print('{:d}\n{:d}'.format(a+b,a-b))
Similarly in the answer we can do
同样在答案中我们可以做
print ("{0}. {1} appears {2} times.".format(22, 'c', 9999))
The python method format() for string is used to specify a string format. So {0},{1},{2} are like array indexes called as positional parameters. Therefore {0} is assigned first value written in format (a+b), {1} is assigned the second value (a-b) and so on. We can also use keyword instead of positional parameter like for example
字符串的python 方法format() 用于指定字符串格式。因此,{0}、{1}、{2} 就像称为位置参数的数组索引。因此,{0} 被赋予以格式 (a+b) 书写的第一个值,{1} 被赋予第二个值 (ab),依此类推。我们也可以使用关键字代替位置参数,例如
print("Hi! my name is {name}".format(name="rashi"))
Therefore name here is the keyword and its value is Rashi Hope it helps :)
因此这里的名称是关键字,它的值是 Rashi 希望它有帮助:)

