在变量之间附加逗号

Cap*_*orn 0 python string

以下是我的代码。我想通过逗号分隔列表附加 ip:port 字符串。

ip = ['1.1.1.1', '2.2.2.2', '3.3.3.3', '4.4.4.4']
memcache = ''
port = '11211'
for node in ip:
    memcache += str(node) + ':' + port
    # join this by comma but exclude last one
Run Code Online (Sandbox Code Playgroud)

我想要这种格式的输出:

memcache = 1.1.1.1:11211, 2.2.2.2:11211, 3.3.3.3:11211, 4.4.4.4:11211

我怎样才能做到这一点?

Ewa*_*wan 5

memcache = ', '.join("{0}:{1}".format(ip_addr, port) for ip_addr in ip)

  • 你忘记了结尾的`"`。 (2认同)