Re: [Tutor] Concatenating Strings
On Tue, May 29, 2012 at 03:51:21PM +1000, Steven D'Aprano wrote: > > Using + is arguably preferable when you have a choice to make, since it > > works in all cases, including constants. > > I'll argue differently: even though + works with string literals as well > as variables, you shouldn't use it. Oops, that sentence is unclear -- what I meant is that you shouldn't use + to concatenate two string literals, not that you should never use it at all! When you have one or both are variables, you MUST use + rather than implicit concatenation. Sorry for any confusion. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Concatenating Strings
On Mon, May 28, 2012 at 07:07:20PM -0700, Steve Willoughby wrote: > On 28-May-12 19:00, Jeremy Duenas wrote: > >and the both printed the same output……so why would I want to use > >‘+’ to > >add strings if there seems to be no reason too? > > Juxtaposing strings only works with constants, which may be convenient > in some cases, but it won't work at all when concatenating other string > values. > > a="hello" > b="world" > > a+b# will yield "helloworld" but > a b# is a syntax error > > Using + is arguably preferable when you have a choice to make, since it > works in all cases, including constants. I'll argue differently: even though + works with string literals as well as variables, you shouldn't use it. Python the language promises that implicit concatenation of literals will be performed at compile time. That is, if you write source code: print("hello " "world") # implicit concatenation any implementation of Python (such as PyPy, IronPython, Jython, etc.) must ensure that the literals "hello " and "world" are concatenated at compile-time into a single string "hello world", not at run-time. But the same is not the case for + concatenation: print("hello " + "world") # explicit concatenation which may be done either at compile-time, or at run-time, depending on the implementation and version of Python, or the presence of optimizations, or the phase of the moon for all we know. Now, normally this won't matter. The nanosecond or so that it takes to concatenate two short strings like that is trivial. Even if it happens at run-time, who will care? But the *intent* is more clear: implicit concatenation clearly states that these substrings belong together, in a way that + doesn't. (At least in my mind.) Furthermore, there are times where run-time concatentation of literals does add some small, but measurable, cost: for i in range(1000): print("hello " + "world") do_something_useful(i) In this case, versions of Python that delay the concatenation to run-time will have to add the two substrings not once, but ten million times. Versions of Python that do it at compile time only need to add them together once. Of course, nobody sensible should be concatenating such small strings like that when they could just write "hello world" as a single string. Why would you bother? But this is a useful feature when you have longer strings: print("This is some longer string, where it is not appropriate to " "use a triple-quoted string because it adds linebreaks, but " "the string is too long to fit on a single line. In this case " "implicit string concatenation is the ideal solution to the " "problem. Don't forget to leave a space at the end of each " "line (or the beginning if you prefer).") In cases like this, the difference between a compile-time concatenation and run-time may be significant, especially inside a fast loop. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Concatenating Strings
Thanks for the replays all your responses makes perfect sense looking at it that way. I agree the example in the book explained it simply, just made no sense as to why and how it is really used. What ya'll have explained to me makes sense and thank you. I can see the importance of it now. From: brian arb [mailto:brianjames...@gmail.com] Sent: Monday, May 28, 2012 7:22 PM To: Jeremy Duenas Cc: tutor@python.org Subject: Re: [Tutor] Concatenating Strings Your right that example from the book is a terrible example the point or the reason to concatenating strings. here is a simple usage of where concatenating strings prints out a simple string as a counter in a loop. >>> for i in range(5): ... print(str(i) + ' in for loop') ... 0 in for loop 1 in for loop 2 in for loop 3 in for loop 4 in for loop On Mon, May 28, 2012 at 10:00 PM, Jeremy Duenas wrote: I am trying to understand the point behind using the '+' character when trying to concatenate strings. I am new to learning Python and going through the book "Python Programming for Absolute Beginners 3rd ed." and do not understand the point or reason for concatenating strings. The reason I do not understand is when experimenting to understand what was being taught I wrote: print("\nThis string" "may not" "seem terr" "ibly impressive") then wrote: print("\nThis string" + "may not" + "seem terr" + "ibly impressive") and the both printed the same output..so why would I want to use '+' to add strings if there seems to be no reason too? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Concatenating Strings
On 28-May-12 19:00, Jeremy Duenas wrote: and the both printed the same output……so why would I want to use ‘+’ to add strings if there seems to be no reason too? Juxtaposing strings only works with constants, which may be convenient in some cases, but it won't work at all when concatenating other string values. a="hello" b="world" a+b# will yield "helloworld" but a b# is a syntax error Using + is arguably preferable when you have a choice to make, since it works in all cases, including constants. -- Steve Willoughby / st...@alchemy.com "A ship in harbor is safe, but that is not what ships are built for." PGP Fingerprint 4615 3CCE 0F29 AE6C 8FF4 CA01 73FE 997A 765D 696C ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Concatenating Strings
Your right that example from the book is a terrible example the point or the reason to concatenating strings. here is a simple usage of where concatenating strings prints out a simple string as a counter in a loop. >>> for i in range(5): ... print(str(i) + ' in for loop') ... 0 in for loop 1 in for loop 2 in for loop 3 in for loop 4 in for loop On Mon, May 28, 2012 at 10:00 PM, Jeremy Duenas wrote: > I am trying to understand the point behind using the ‘+’ character when > trying to concatenate strings. I am new to learning Python and going > through the book “Python Programming for Absolute Beginners 3rd ed.” and > do not understand the point or reason for concatenating strings. The reason > I do not understand is when experimenting to understand what was being > taught I wrote: > > ** ** > > print(“\nThis string” “may not” “seem terr” “ibly impressive”) > > ** ** > > then wrote: > > ** ** > > print(“\nThis string” + “may not” + “seem terr” + “ibly impressive”) > > ** ** > > ** ** > > and the both printed the same output……so why would I want to use ‘+’ to > add strings if there seems to be no reason too? > > ** ** > > ** ** > > ** ** > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Concatenating Strings
On 05/28/2012 10:00 PM, Jeremy Duenas wrote: > I am trying to understand the point behind using the '+' character when > trying to concatenate strings. I am new to learning Python and going through > the book "Python Programming for Absolute Beginners 3rd ed." and do not > understand the point or reason for concatenating strings. The reason I do > not understand is when experimenting to understand what was being taught I > wrote: > > > print("\nThis string" "may not" "seem terr" "ibly impressive") > > > then wrote: > > > > print("\nThis string" + "may not" + "seem terr" + "ibly impressive") > > > > > and they both printed the same output..so why would I want to use '+' to add > strings if there seems to be no reason too? > > > As long as they're all literal strings, you can use the implicit concatenation. Or you can just make one longer string. Makes no difference, other than white space and sometimes prettiness. However, as soon as one of the strings is a variable of type "str" then you need the "plus sign." -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Concatenating Strings
I am trying to understand the point behind using the '+' character when trying to concatenate strings. I am new to learning Python and going through the book "Python Programming for Absolute Beginners 3rd ed." and do not understand the point or reason for concatenating strings. The reason I do not understand is when experimenting to understand what was being taught I wrote: print("\nThis string" "may not" "seem terr" "ibly impressive") then wrote: print("\nThis string" + "may not" + "seem terr" + "ibly impressive") and the both printed the same output..so why would I want to use '+' to add strings if there seems to be no reason too? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor