将整个句子转换为字符串而不分割成列表

问题描述 投票:-1回答:3

我正在尝试将句子转换为列表格式而没有空格分隔符我尝试过以下方式

a = 'this is me'

当我使用split进入列表格式时

a.split(' ')
# ['this', 'is', 'me']

list(a)
# ['t','h','i','s','m','e']

有没有办法将输入作为

a = 'this is me'

并获得输出为

a = ['this is me']
python python-3.x string list
3个回答
1
投票

使用此:-

>>> a = 'this is me'
>>> [a]
['this is me']

使用list使函数在不需要的字符串上进行迭代。请改用那些大括号作为列表构造函数。


0
投票
IN: a= 'this is me'
IN: a = [a]
IN: print(a)
OUT: ['this is me']

0
投票

" ".join(a.strip().split(" "))

© www.soinside.com 2019 - 2024. All rights reserved.