What would be the most efficient way to add each string

When defining

message =

What would be the most efficient way to add each string to the message variable.

Am I overthinking it? Are there situations while programming where you need to define a variable with concatenating say, 10+ strings?

This exercise wants you to write each string out manually.

message = string1 + string2 + string3 + string4 + string5 + string 6

Which isn’t a problem, but just curious if there is a more efficient way to write it.

There are other ways to concatenate strings.
If its a list of strings, you may want to use .join

list_of_strings = [string1, string2, string3, string4, string5, string6]
message = "".join(list_of_strings)

Loops are another way to do that:

list_of_strings = [string1, string2, string3, string4, string5, string6]
message = ""
for string in list_of_strings:
  message += string
1 Like

Hello,

The strings were not a part of a list though. They were individually written out as:
string1 =
string2 =
string3 =

and so on.

So is there a quick way to group all these variables together with some type of loop or something, or would a copy and paste method into a list be the fastest?

You don’t have to add them into a list to use .join, it just has to be an iterable of some form, "".join(("a", "b", "c",)). Bear in mind that most containers woudln’t copy the data itself, just references to the data. When you say copy-paste do you mean the variable names or the string contents? Because a string literal would arguably be better still but I don’t think that’s what you’re looking for.

The problem with the loop style concatenation += or + is that strings are immutable in Python, so each time you add a new string you get a new string object which can add up to an awful lot of overhead very quickly in Big O terms (for three short strings that might not matter much at all though).

I’d hazard a guess that .join that janbazant mentioned before would be the best option under the greatest number of circumstances (it’s a single line, readable and implemented in C). You’d really need to start profiling them if you had to push for more efficiency than that.


Edit:
Just cottoned on to the fact you’re asking about writing it efficiently rather than execution wise (I think).

For the most part you probably won’t ever have hundreds of uniquely named strings (unless someone made some terrible design decisions) and it’s much more likely they’d be in a container (a list or similar) already. If you do find yourself in that position consider refactoring the code that led to that situation.

If you had to write it out then your options are like those below (by no means an exhaustive list if you’re feeling creative!). Personally I’d be a fan of option #1 in this case because it’s pretty obvious what’s going on.

#1
message = string1 + string2 + string3 + string4 + string5 + string6
#2
message = "".join((string1, string2, string3, string4, string5, string6,))
#3
message = f"{string1}{string2}{string3}{string4}{string5}{string}"
#4
message = ""
for partial_msg in (string1, string2, string3, string4, string5, string6,):
    message += partial_msg

If you actually meant execution efficiency then you’d probably want to profile these (but I don’t think #4 is particularly efficient or readable).

Thanks! I think I found the answer to my question, but have asked it in a poorly worded way lol.

Also, I meant copy-paste the variable names.
As in, copy and paste each variable name into my new variable, while using string concatenation. Because for the exercise, you have to concatenate each of the six variables into a new variable. For 6 variables, its not much to write out each variable name, but then I thought of a hypothetical situation where you would have maybe a hundred variables that would need to be concatenated into a new variable (which seems to be an unlikely scenario based on what you said). Hope I am making sense. I assume I’m probably overthinking the possible ways I would use concatenation…

Example of what I mean:

#100 different string variables

String1 = “something here”
string2 = “something else”

String100 = “end of 100 strings”

#new variable including all 100 strings
message = string1 + string2 + … string100
print(message)

I imagine this is an unlikely scenario…where I would have 100 variables concatenated into a new single variable?

1 Like

Hehe aye, hopefully a very unlikely scenario.

Depending on the names you could do some tricks with text search/replace, regex and such to perhaps make the new list for you. A nice IDE can make that operation quite fast (but may be useless for only a couple of names). Getting used to search/replace and even basic regex is probably worthwhile in the long term.

At the end of the day though it seems like something must have gone wrong beforehand to lead to a situation where 100 unique names are being squeezed together :grinning_face_with_smiling_eyes:.

Thank you for the clarification!

1 Like