在不使用spli的情况下将整个句子转换成字符串

2024-04-24 22:00:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将一个句子转换成没有空格分隔符的列表格式 我试过下面的方法

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']

Tags: 方法列表is格式thislist句子split
2条回答

a = 'this is me'

" ".join(a.split(" "))将返回'this is me',如果a有前导/尾随空格a = ' this is me ',则可以使用" ".join(a.strip().split(" "))

为了得到你的解决方案

a = [a.strip()]

使用本文件:你知道吗

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

使用list使函数在字符串上迭代,这是您不想要的。改用那些大括号,作为列表构造函数。你知道吗

相关问题 更多 >