+ 1

When to use , or + in python?

So I just recently did this beginner challenge: country = input() capital = input() print("Country:", country) print("Capital:", capital) Initially, I tried to put: print("Country:" + country) I'm trying to figure out why this would be incorrect. Edit: So it seems my initial answer does work, it just doesn't put the space between 'Country:' and 'CountryName' so it marked it as wrong.

25th Jan 2023, 3:45 PM
Esco
3 ответов
+ 8
Compare the 2 versions: Is there a blank space after ":"? The output strings must be exactly like in the task description.
25th Jan 2023, 3:54 PM
Lisa
Lisa - avatar
+ 7
When you pass comma separated values to print then, those are displayed by space separated on the output console. But the later of using + , don't adds space. Check output. Ex: print('a', 'b') #a b print('a' + 'b') #ab
25th Jan 2023, 3:55 PM
Jayakrishna 🇮🇳
0
When to use , or + + works almost the same as , if both are strings. For other types, different things may happen. For example, if both are integers or floats (numbers), it adds them together. If both are not of the same type, it usually generates an error. Why? Because + is not actually part of the print function. You add the two things together, whatever that may mean for the data you're working with, and then feed the result to the print function as a single argument. + puts two strings right after each other without space. , adds a space. This can be avoided, if needed, by the optional argument sep (for separator). Try these examples: print("wit", "hout", sep = "") print("with", 0, "spaces", sep = "")
29th Jan 2023, 12:49 AM
Marijke van Bommel
Marijke van Bommel - avatar