Why we concatenating string by +?


#1

print 'hello '+'world' and print 'hello ''world'
both are available.


#2

Two string literals next to each other without any operator will be read as a single string, they don't get concatenated, it's be a single string as soon as the code is read (before it is run)

So it's just a syntax feature.

If you actually needed to concatenate two strings, then you would need + (or equivalent).

For example, this isn't going to work:

string1 = 'hello '
string2 = 'world!'

print(string1 string2)

#3

hi!thank you for your reply.do you means this code:print 'hello ''world' is a single string in python?If I work on two string should be concatenated them by+?Looking forward to your reply´╝îthank you!


#4

@lilei007 Yup you got that correct.


#5

Thank you very much! :grinning: