如何在for循环中连接这些字符串变量?

时间:2019-04-18 10:53:22

标签: python

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

我尝试过:

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

但是它不起作用并显示错误:字符串未定义

我想直接操作var,我知道将它们放在列表中要容易得多,但是想像一下,如果您有1000个字符串,则很难在列表中编写每个字符串。

6 个答案:

答案 0 :(得分:1)

使用join()

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

print("".join(cList))

我建议的变量应该是列表中的变量,而不是n个变量:

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))

单线:

print("".join([string1, string2, string3, string4, string5, string6]))

答案 1 :(得分:0)

如果首先可以将字符串放在列表中,那就更好了。否则:

for s in [string1, string2, string3, string4, string5, string6]:
    message += s

答案 2 :(得分:0)

您正在使用不同的变量。您将必须分别调用每个变量才能将它们连接起来,因为您正试图像在列表中那样调用它们。尝试将它们添加到数组或列表中。

答案 3 :(得分:0)

也许您在寻找eval吗?

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

答案 4 :(得分:0)

只需一口气写下您的故事(请注意字符串之间没有逗号):

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

或者如果您想输入较少的引号:

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

答案 5 :(得分:0)

对此问题有多种解决方案,评论中给出了一种。您收到该错误的原因是因为字符串不存在。您正在致电string(i),您究竟希望这样做吗?

此外,您正在执行的循环逻辑不正确。当进行编码但未获得预期结果时,第一道防线是调试。在这种情况下,请了解您要遍历的内容,本质上是数字。继续并打印该 i 变量,以便您了解发生了什么。您完全可以访问 stringX 变量。它们必须包含在可迭代中,以便您对其进行迭代。更不用说 for循环迭代是错误的,因为 range(x)提供了从 0 x-1 ,在您的情况下为 0 1 2 3 4 5 。您将知道,如果您已调试。我必须说,编码的一部分是调试。习惯去做是一件好事。

这里是documentation on Python about strings.

  

string.join(words [,sep])   将单词的列表或元组与中间出现的sep连接起来。 sep的默认值是单个空格字符。始终是string.join(string.split(s,sep),sep)等于s。

这意味着您可以使用字符串的方法join来连接字符串。该方法要求您向其传递字符串的列表。您的代码如下所示:

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)

输出:

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.

由于我不确定您的目标是什么,因此,为了有所不同,我将假设您有任意数量的字符串变量传递给您。这甚至更容易处理,因为如果定义一个名为 join_strings 的方法,则传递的变量的值已经是一个列表。整洁吧?因此,您的代码将类似于:

def join_strings(*strings):
    return "".join(strings)

非常矮小又甜蜜,不是吗?然后,您将像这样调用该方法:

join_strings(string1, string2, string3, string4, string5, string6)

很棒的事情是明天您可能只有3个字符串或8个字符串,并且仍然可以使用。当然,知道为什么还要保存这样的字符串会更有用,因为我确信您可以根据需要使用更合适的数据结构(例如使用列表开头)。

下次您发布到StackOverflow时,最好尝试展示一些尝试做的事情来理解您的问题,而不仅仅是粘贴问题。