[英]Printing variables in Python 3.4
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 variables correctly?如何更改我的打印语句以正确打印变量?
The syntax has changed in thatprint
is now a function .语法已经改变,print
现在是一个函数。 This means that the %
formatting needs to be done inside the parenthesis: 1这意味着%
格式需要在括号内完成: 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.format
method:但是,由于您使用的是 Python 3.x.,您实际上应该使用较新的str.format
方法:
print("{}. {} appears {} times.".format(i, key, wordBank[key]))
Though %
formatting is not officially deprecated (yet), it is discouraged in favor of str.format
and will most likely be removed from the language in a coming version (Python 4 maybe?).尽管%
格式尚未正式弃用(尚未),但不鼓励使用str.format
并且很可能会在即将到来的版本中从语言中删除(也许是 Python 4?)。
1 Just a minor note: %d
is the format specifier for integers, not %s
. 1只是一个小注意: %d
是整数的格式说明符,而不是%s
。
版本 3.6+:使用格式化字符串文字,简称f-string
print(f"{i}. {key} appears {wordBank[key]} times.")
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.打印函数就像任何其他函数一样被调用,它的所有参数都用括号括起来。
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.
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])))
one can print values using the format method in python.可以使用 python 中的 format 方法打印值。 This small example will help take input of two numbers a and b.这个小例子将有助于输入两个数字 a 和 b。 Print a+b in first line and ab in second line在第一行打印 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.字符串的python 方法format() 用于指定字符串格式。 So {0},{1},{2} are like array indexes called as positional parameters.因此,{0}、{1}、{2} 就像称为位置参数的数组索引。 Therefore {0} is assigned first value written in format (a+b), {1} is assigned the second value (ab) and so on.因此,{0} 被赋予以格式 (a+b) 书写的第一个值,{1} 被赋予第二个值 (ab),依此类推。 We can also use keyword instead of positional parameter like for example我们也可以使用关键字代替位置参数,例如
print("Hi! my name is {name}".format(name="rashi"))
Therefore name here is the keyword and its value is Rashi Hope it helps :)因此这里的名称是关键字,它的值是 Rashi 希望它有帮助:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.