3 Replies - 225 Views - Last Post: 11 July 2013 - 05:15 AM Rate Topic: -----

#1 datus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 08-July 13

difference between two?

Posted 10 July 2013 - 07:37 AM

What is the difference between these two statements?.....i mean without the concatenation operator '+' python still concatenates two strings...why is that so?
>>> print('hello'+'world')
helloworld
>>> print('hello' 'world')
helloworld


Is This A Good Question/Topic? 0
  • +

Replies To: difference between two?

#2 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7564
  • View blog
  • Posts: 12,688
  • Joined: 19-March 11

Re: difference between two?

Posted 10 July 2013 - 07:55 AM

I suspect the no-operator concatenation is just syntactic sugar. Python tries to be a DWIM language, and it's usually pretty good at it.


Looking around to see if I could find anything more, I found this which was interesting (though not directly related to your question)
Was This Post Helpful? 0
  • +
  • -

#3 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 116
  • View blog
  • Posts: 212
  • Joined: 11-January 13

Re: difference between two?

Posted 10 July 2013 - 11:31 PM

There was a pep submitted a while back proposing that implicit string concatenation be removed from the language.
PEP 3126
It was rejected.

I'm personally thankful that it was rejected as I feel it has its place. The most common use case is for strings that would span multiple lines. It allows you to span multiple lines without using the (highly discouraged) line continuation "\". You can of course use triple quotes as well to this end, but there are a couple of reasons not to. Firstly, triple quotes preserve all white space, so you can not align an indented block of text in your code without adding whitespace that will appear in the output string. Secondly, triple quotes are highly associated with docstrings, and perhaps best reserved for this use.

See the accepted answer here for a little more on the subject:
How can I break up this long line in python

-Mek

This post has been edited by Mekire: 11 July 2013 - 08:17 AM

Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7564
  • View blog
  • Posts: 12,688
  • Joined: 19-March 11

Re: difference between two?

Posted 11 July 2013 - 05:15 AM

Good context, thanks.

Of course, the practical programmer in me wonders why the hell you would have multi-line string literals in your program that aren't stored exterally as program data, but that's probably another argument for another time.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1