-3
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."

I tried:

for i in range(6): 
     message +=string(i)

but it didn't work and showed the error: string is undefined

I want to manipulate the vars directly, I know that putting them in a list is much easier, but imagine if you had like 1000 string, kinda difficult to write each one in the list.

ruohola
  • 21,987
  • 6
  • 62
  • 97
Nazim HS
  • 1
  • 1
  • 1
    Possible duplicate of [Which is the preferred way to concatenate a string in Python?](https://stackoverflow.com/questions/12169839/which-is-the-preferred-way-to-concatenate-a-string-in-python) ... please spend 5-10 minutes searching Stack Overflow before you post a question. – Tim Biegeleisen Apr 18 '19 at 10:54
  • 2
    You should just use a list rather than n number of variables with undescriptive names – Sayse Apr 18 '19 at 10:54
  • 2
    Why not have them in a list and `join()` instead? – DirtyBit Apr 18 '19 at 10:55
  • no i want a solution for this particular problem , does it exist ? – Nazim HS Apr 18 '19 at 10:56
  • Have them all in alist and iterate over it? – DirtyBit Apr 18 '19 at 10:57
  • 1
    Your edit doesn't make any sense, you'll never have 1000 strings. If you did they'd be in a list, or in a single long string which may also make more sense – Sayse Apr 18 '19 at 11:04
  • In `string(i)` you are trying to compute the name of a variable, as in `string1`. The way to do that in Python is to put the strings in a `list` or a `dict`. If you had 1,000 strings you would be reading them from a file and putting them in a data structure, not typing them in by hand. So take the advice you are being offered to abandon this approach. – BoarGules Apr 18 '19 at 12:05

6 Answers6

1

Using join():

cList = [string1, string2, string3, string4, string5, string6]

print("".join(cList))

What I'd suggest, instead of a n number of variables, have them in a list:

x = ["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."]    

print("".join(x))

One-liner:

print("".join([string1, string2, string3, string4, string5, string6]))
DirtyBit
  • 16,613
  • 4
  • 34
  • 55
0

If you can put the strings in a list in the first place, so much the better. Otherwise:

for s in [string1, string2, string3, string4, string5, string6]:
    message += s
brunns
  • 2,689
  • 1
  • 13
  • 24
0

You are using different variables. You would have to individually call each variable to be able to concatenate them, because you are trying to call them like you would in a list. Try adding them to an array, or list.

Andrew R.
  • 72
  • 7
0

maybe you were looking for eval ?

 message = ''.join([eval('string'+str(i)) for i in range(1,7)])
gherkin
  • 476
  • 7
  • 24
0

Just write your story in one go (note the lack of commas between the strings):

message = ("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.")

Or if you want to type less quotes:

import inspect

message = """
          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.
          """

message = inspect.cleandoc(message)  # remove unwanted indentation
message = message.replace('\n', ' ')  # remove the newlines
ruohola
  • 21,987
  • 6
  • 62
  • 97
0

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.

Chayemor
  • 3,577
  • 4
  • 31
  • 54