【问题标题】:How can I convert a string to a list in Python using a for loop? [duplicate]如何使用 for 循环将字符串转换为 Python 中的列表? [复制]
【发布时间】:2016-07-22 13:55:15
【问题描述】:

这是我所拥有的:

def 反向(文本): 对面 = [] 对于文本中的字符: 东西=对面。附加(字符) 退货

这个for循环有问题吗?

【问题讨论】:

    标签: string list function for-loop


    【解决方案1】:

    你不能使用 split 方法,它会返回一个列表

    text.split()
    

    甚至

    list(text)
    

    列出每个字符

    【讨论】:

      【解决方案2】:
      yourstring='welcome'
      l=[]
      for char in yourstring:
            l.append(char)
      print l
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-07-06
        • 2011-11-23
        • 2019-10-21
        • 2022-11-25
        • 2021-11-13
        • 2021-02-24
        • 2022-01-21
        • 2013-02-28
        相关资源
        最近更新 更多