Inserting a string into a list without getting split into characters

后端 未结 9 1816
孤城傲影
孤城傲影 2020-12-13 16:45

I\'m new to Python and can\'t find a way to insert a string into a list without it getting split into individual characters:

>>> list=[\'hello\',\'w         


        
9条回答
  •  [愿得一人]
    2020-12-13 17:28

    Don't use list as a variable name. It's a built in that you are masking.

    To insert, use the insert function of lists.

    l = ['hello','world']
    l.insert(0, 'foo')
    print l
    ['foo', 'hello', 'world']
    

提交回复
热议问题