3 ответов
+ 7
The reason why , separate strings with whitespaces is because of its parameter "sep" and its value is whitespace by default.
print("This", "is", "a", "string")
>> This is a string
print("This " + "is " + "a " + "string")
>> This is a string
On the second one (concatenation, +) notice that there is a whitespace at the end of each substring, in order to separate each word when concatenating.
Another example using "sep" parameter:
print("This", "is", "a", "string", sep="@@")
>> This@@is@@a@@string
- - - - - - - - - - - - - - - - - - - -
Although , is not really concatenating the string, it is just used for "print()" format and arguments, for example you can do this:
print("string", 42, True)
>> string 42 True
So Abhay and get is right about "," does not join string, but "+" does.
- - - - - - - - - - - - - - - - - - - - -
Though I prefer f-strings for printing strings (you can even place variables in it)
var = "Black"
print(f"color is {var}")
>> color is Black
+ 4
"," doesn't joins the string !
+ 3
", " actually does not works like you think: it is just arguments/elements listing.
As I guess, you are talking about "print" function: it prints arguments (in "in print('hello', 'world')" 'hello' and 'world' are two arguments) separated by some specific string (default is " ", but you can set your own to this printing by ", sep = <your separator>" after all the "unnamed arguments") and with some specific string at the end (without separator; default is '\n', but you can set your own to this printing by ", end = <your ending>). That is it.
And "+" operator "adds" two objects: adds numbers, concatenates strings, etc. But there is one detail which important (especially for beginners in programming): string concatenation does not add the space before those strings, it just joins them ("hello" + "world" = "helloworld", "hello " + "world" = "hello world").
Hope this helped.
P. S.: If you do not understand the first paragraph, just keep learning and you will "meet" functions.