【问题标题】:How to copy list into string in python? [duplicate]如何在python中将列表复制到字符串中? [复制]
【发布时间】:2021-05-04 17:17:26
【问题描述】:

list = ['伦敦', '巴黎', '北京']

我想转换成与列表相同的字符串。

示例: str = "['伦敦', '巴黎', '北京]"

【问题讨论】:

    标签: python string list


    【解决方案1】:
    l = ['london', 'paris', 'beijing']
    s = str(l)
    print(s) # ouput: ['london', 'paris', 'beijing']
    

    【讨论】:

    • 我有错误:TypeError: 'str' object is not callable
    • 这是因为您将字符串变量命名为str,就像内置函数一样。尝试将其更改为 sstring 就像我在答案中所做的那样
    • >>> l = ['london', 'paris', 'beijing'] >>> s = str(l) Traceback(最近一次调用最后):文件“”,第 1 行,在 TypeError: 'str' object is not callable
    • 你是不是先重启了shell?
    • 重启后就可以了。非常感谢。
    猜你喜欢
    • 2020-08-02
    • 2011-11-23
    • 2018-09-25
    • 2014-02-16
    • 1970-01-01
    • 2013-02-28
    • 1970-01-01
    • 2020-10-09
    • 1970-01-01
    相关资源
    最近更新 更多