What is the difference between printing two variables using “+” and “,” in Python?

前端 未结 1 1358
你的背包
你的背包 2021-01-29 03:19
a = 2
b = 4
print(a, b)
print(str(a) +" " + str(b))

Is there a difference between the first print and the second print? If there is, which one

1条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-29 03:54

    print(a, b) uses a and b as function parameters.

    print(str(a) + str(b)) actually concatenates strings and then passes result to print() function.

    That's the only difference.

    But you can get the advantage from print(a, b) by using sep argument. This can be helpful when you're passing multiple arguments and you want all of them to be separated with certain text.

    Ex:

    a, b, c = 1, 2, 3
    print(a, b, c, sep='--')   # prints 1--2--3
    

    0 讨论(0)
提交回复
热议问题