Concatenated strings
Hey there, I'm learning the python program language from a book, and I'm on a section explaining concatenated strings. I understand how to do them, but I can't see why you'd want to use them! Can anyone explain where and why you'd use one? Thanks!
print ("This string " + "is concatenated!")
Sign in to comment in this discussion.
Comments
I see, could you give a short example? I'm fairly new to python
Assuming that the variables userName & date were declared both as a string and that the program asked the user to enter their name and it was stored in userName (the date would be retrieved by an automatic call) then they could be use to simplify the output of the data in a nice clean fashion using code like this:
That code would store "Hello name. Today is date." which could be displayed with ease or even added to like this:
Do you get what I mean?
The example you gave isn't a good use of it, as it's the same as just typing the whole sentence inside the quotes but it does demonstrate what it does.
For Python, (as mentioned above), being able to concatenate two string variables can come quite in handy.
Here's some examples:
I've only been working in Python for about six months (but C/C++ for 10 years before that, plus shell scripting, perl and other bits and pieces) and I love it. Python is quite a great language to work with.
Feel free to try out all of the above. The good thing about Python is you can run the interactive python console and try things out, without having to first write the whole thing up in a text file and compile it.
For example, you could rewrite your example as:
Sorry if the code is wrong, it's been ages since I've used Perl. Of course, using ?: makes it much shorter
As far as I know, ?: is NOT available in Python, but it has several other language properties that make up for its omission
in Python, since a boolean defaults to 0 for false and 1 for true.
This is interpolation in Perl:
Where $scalar is a scalar variable and @list is an array that gets interpolated into the string. You can also set the global $, variable to anything other than commas if you want something else to separate elements in the list on interpolation.
Of course, you can do all sorts of neat stuff with interpolation, like putting expressions in @{[]} brackets inside the string, but I usually avoid that, since interpolation is a clean way of creating strings, and that usually just clutters it.
Thanks for the python hint, bamse, it might come in handy
EDIT: Have done some thinking (and more coding, but in C++) today, and think your example would be better rendered with the choices in a tuple, using the [] operator as an index.
It'd also be valid to use a list with the [] operator, but probably not the () operator.
That is, the following two are valid:
but bamse's code isn't quite...
Yeah, I think your tuple variant is what I was thinking of. Like I said, I haven't used python for some years now. Are tuples circular?
I think that's the main difference. I don't think they are circular.