There are various solutions for this problem, one was given in the comments. The reason you are getting that error is because string doesn't exist. You are calling string(i)
, What exactly are you expecting that to do?
Also, the loop you are doing doesn't have the right logic. When coding and not getting the expected result, the first line of defence is to debug. In this case, understand what you are looping through, which essentially is numbers. Go ahead and print that i variable so you can see what's happening. You are accessing your stringX variables at all. They need to be contained in an iterable in order for you to iterate them. Not to mention that the for loop iteration is wrong since range(x) provides numbers from 0 to x-1, which in your case would be 0 1 2 3 4 5. You would have known that if you had debugged. Part of coding, I must say, is debugging. It's something good to get used to doing.
Here's the documentation on Python about strings.
string.join(words[, sep])
Concatenate a list or tuple of words with intervening occurrences of sep. The default value for sep is a single space character. It is always true that string.join(string.split(s, sep), sep) equals s.
That means you can use the string's method join to concatenate strings. The method requries you pass it a list of string. Your code would look like this:
string1 = "The wind, "
string2 = "which had hitherto carried us along with amazing rapidity, "
string3 = "sank at sunset to a light breeze; "
string4 = "the soft air just ruffled the water and "
string5 = "caused a pleasant motion among the trees as we approached the shore, "
string6 = "from which it wafted the most delightful scent of flowers and hay."
message = "".join([string1, string2, string3, string4, string5, string6])
print(message)
Output:
The wind, which had hitherto carried us along with amazing rapidity, sank at sunset to
a light breeze; the soft air just ruffled the water and caused a pleasant motion among the
trees as we approached the shore, from which it wafted the most delightful scent of
flowers and hay.
Since I am not sure what you goal is, I am going to assume, for the sake of having something different, that you have an arbitrary number of string variables passed to you. That's even easier to handle, because if you define a method called join_strings the value of the variables passed is already a list. Neat, right? So your code would be something like:
def join_strings(*strings):
return "".join(strings)
Incredibly short and sweet isn't it? Then you would call that method like this:
join_strings(string1, string2, string3, string4, string5, string6)
The cool thing is that tomorrow you might have only 3 strings, or 8, and that still works. Of course, it'd be more helpful to know why you are even saving the strings like that, since I'm sure you can use a better suited data structure for your needs (like using a list to begin with).
Next time you post to StackOverflow, it's good to try and show some effort of what you have tried to do to understand your problem instead of just pasting the problem.