Kartik Mishra
Kartik Mishra

Reputation: 41

What is point of using the concatenation(+) in Python when commas(,) do the job?

print("This is a string" + 123)

Concatenating throws error, but using a comma instead does the job.

Upvotes: 3

Views: 1365

Answers (6)

Jaime R
Jaime R

Reputation: 1

# You can concatenate strings and int variables with a comma, however a comma will silently insert a space between the values, whereas '+' will not. Also '+' when used with mixed types will give unexpected results or just error altogether.

>>> start = "Jaime Resendiz is"
>>> middle = 21
>>> end = "years old!

>>> print(start, middle, end)

>>> 'Jaime Resendiz is 21 years old!'

Upvotes: 0

AleksanderGD
AleksanderGD

Reputation: 408

If you have your int value in a variable, you can print it out with f-string (format string). Format take more inputs like print(("one text number {num1} and another text number {num2}").format(num1=variable1, num2=variable2)

x = 123
print(("This is a string {x}").format(x=x))

The above code outputs:

This is a string 123

You can read more about it here: python-f-strings

Upvotes: 0

Ice Bear
Ice Bear

Reputation: 4086

It's simple cause 123 is an int type and you cannot concatenate int with str type.

>>> s = 123
>>> type(s)
<class 'int'>
>>>
>>> w = "Hello"+ s
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str
>>>
>>>
>>> w = "Hello" + str(s)
>>>
>>> w
'Hello123'
>>>

You can see the error , so you can convert the s variable that its value is 123 to string using str() function. But situations like this that you want to concatenate strings with other types? I think you should use f-strings

Example

>>> boolean = True
>>> fl = 1.2
>>> integer = 100
>>>
>>> sentence = f"Hello variables! {boolean} {fl} {integer}"
>>> sentence
'Hello variables! True 1.2 100'

Upvotes: -1

Roberto Caboni
Roberto Caboni

Reputation: 7490

As you already been told, your code raises an error because you can only concatenate two strings. In your case one of the arguments of the concatenation is an integer.

print("This is a string" + str (123))

But your question is more something "plus vs. comma". Why one should ever use + when , works?

Well, that is true for print arguments, but actually there are other scenario in which you may need a concatenation. For example in an assignment

A = "This is a string" + str (123)

Using comma, in this case, would lead to a different (and probably unexpected) result. It would generate a tuple and not a concatenation.

Upvotes: 4

tevemadar
tevemadar

Reputation: 13225

That's one case of print(). However if you do need a string, concatenation is the way:

x = "This is a string, "+str(123)

gets you " This is a string, 123"

Should you write

x = "This is a string", 123

you would get the tuple ("This is a string",123). That's not a string but an entirely different type.

Upvotes: 1

Ujjwal Agrawal
Ujjwal Agrawal

Reputation: 998

Hey here you are trying to concatenate the string and integer. It will throw type error. You can try something like

print("This is a string"+str(123))

Commas (,) are not actually concatenating the values it's just printing it in a fashion that it looks like concatenation. Concatenation on the Other hand will actually join two strings.

Upvotes: 3

Related Questions