简体   繁体   English

print(“str”, a) 和 print(“str” + a) 的区别

[英]difference between print(“ str”, a ) and print(“str” + a )

I am a beginner with python3 and I use a lot print or the logging module to follow the code on the console.我是 python3 的初学者,我使用大量printlogging模块来跟踪控制台上的代码。 A simple example below: what's the difference between:下面是一个简单的例子:有什么区别:

number = "seven"
print("I cooked " , number , " dishes")

and

number = "seven"
print("I cooked " + number + " dishes")

The print statement supports multiple ways of parsing values. print语句支持多种解析值的方式。

number = 'seven'

Examples: Different style of adding argument in print statement示例:在打印语句中添加参数的不同样式

print("I cooked " , number , " dishes")

C-Style formatting (old): C 样式格式(旧):

print("I cooked %s dishes" % number)

C-Style formatting (new) using fomat :使用fomat的 C 样式格式(新):

print("I cooked {} dishes ".format(number))

f-string style f弦风格

print(f"I cooked {number} dishes")

String concatenating:字符串连接:

print("I cooked " + number + " dishes")

You don't necessary to stick with one style.你不必坚持一种风格。 You have various options of doing the same.你有不同的选择来做同样的事情。

Internally the difference is that this example (no + sign):在内部,不同之处在于此示例(无 + 号):

number = "seven"
print("I cooked " , number , " dishes")

Is printing 3 separate string objects.正在打印 3 个单独的字符串对象。 "I cooked " , is object 1, number is object 2, and " dishes" is object 3. So this has 3 objects total. "I cooked " ,是 object 1, number是 object 2, " dishes"是 object 3。所以这总共有 3 个对象。

In the second example (with the + sign):在第二个示例中(带有 + 号):

number = "seven"
print("I cooked " + number + " dishes")

the 3 separate strings are first being concatted into 1 new string object before being printed to stdout.在打印到标准输出之前,首先将 3 个单独的字符串连接成 1 个新字符串 object。 So this example has 4 objects total.所以这个例子总共有 4 个对象。

The operator + can be only used on strings.运算符+只能用于字符串。 The operator , can be used on any type, and adds a space before automatically.运算符,可用于任何类型,并自动在前面添加一个空格。 In addition, + can be used not only in printing but to add one string to another while , cant.此外, +不仅可以用于打印,还可以将一个字符串添加到另一个字符串 while , cant 。

(Note that in the two examples you have, by simply running them will show that the results are different, as the first one will have some words separated by double spaces.) (请注意,在您拥有的两个示例中,通过简单地运行它们将显示结果不同,因为第一个示例将有一些单词用双空格分隔。)

The print() function will take in strings, each string will be printed out with a ' ' between them: print() function 将接受字符串,每个字符串将打印出来,它们之间有一个' '

print('hello', 'world')

Output: Output:

hello world

That is because of the keyword argument, sep .这是因为关键字参数sep By default, sep=' ' , that is changeable by simply adding:默认情况下, sep=' ' ,只需添加以下内容即可更改:

print('hello', 'world', sep='\n')

Output: Output:

hello
world

The + operator will not add any separator, it will simply concatenate the strings: +运算符不会添加任何分隔符,它只会连接字符串:

print('hello' + 'world')

Output: Output:

helloworld

As per PEP3105 print is considered as a function taking *args (several positional arguments).根据PEP3105打印被视为采用 *args (几个位置参数)的 function。

To answer your question, the result is the same;回答你的问题,结果是一样的; however, your implementation is different.但是,您的实现是不同的。 In the first case you give print multiple arguments to print, while in the second case you give print a concatenated string that you would like to print.在第一种情况下,您给 print 多个 arguments 进行打印,而在第二种情况下,您给 print 一个您想要打印的串联字符串。

when using使用时

number = "seven"
print("I cooked " , number , " dishes")

print gets 3 different objects (3 strings) as arguments, converts them to string and then prints. print获取 3 个不同的对象(3 个字符串)作为 arguments,将它们转换为字符串然后打印。

However using但是使用

number = "seven"
print("I cooked " + number + " dishes")

means that first these three strings are concatenated and then passed as one object to print .意味着首先将这三个字符串连接起来,然后作为一个 object 传递给print

In reality, it means, that if you do for example实际上,这意味着,例如,如果您这样做

print('xxx' + 5 + 'yyy')

it will throw and error, as it is not possible to directly concatenate string and int types.它会抛出错误,因为无法直接连接 string 和 int 类型。

Also note following example:另请注意以下示例:

#concatenating 3 strings and passing them as one argument to print
>>> print('xxx' + 'a' + 'yyy',sep=',')
xxxayyy

#passing 3 strings as 3 arguments to print
>>> print('xxx','a','yyy',sep=',')
xxx,a,yyy

You can notice, that in first example, although sep is used (thus it should separate 3 strigns with given separator) it does not work, because these strings are concatenated first and then passed as one argument to print .您可以注意到,在第一个示例中,尽管使用了sep (因此它应该使用给定的分隔符分隔 3 个字符串)但它不起作用,因为这些字符串首先连接起来,然后作为一个参数传递给print In the second example however, strings are passed as separated arguments, therefore sep=',' works, because print just knows that it should pass the separator between each given string.然而,在第二个示例中,字符串作为分隔符 arguments 传递,因此sep=','有效,因为print只知道它应该在每个给定字符串之间传递分隔符。

Lets say a = "how are you" and b = "goodbye" which are 2 string variables.假设a = "how are you"b = "goodbye"是 2 个字符串变量。

  1. If we do print(a, b) :如果我们做print(a, b)

The statement will print first a then b as separate strings outputted on one line:该语句将首先打印a然后b作为单独的字符串输出在一行上:

Output: Output:

> how are you goodbye

  1. If we do print(a + b) the statement will concatenate these two variables a and b together:如果我们执行print(a + b) ,该语句会将这两个变量ab连接在一起:

Output: Output:

> how are yougoodbye (here there's no spacing due no white spacing in the print statement or the variables) > how are yougoodbye (这里没有空格,因为打印语句或变量中没有空白)

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

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