如何在Python中将字符串拆分为不包含空格的单词?
我的字符串是:
"This is a string"
我想把它变成一个列表:
["This", "is", "a", "string"]
我使用 split(" ")
方法,但它添加空格作为列表元素。请帮忙,
最好的问候
My string is:
"This is a string"
I want to turn it into a list:
["This", "is", "a", "string"]
I use the split(" ")
method, but it adds whitespaces as list elements. Please help,
Best Regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只需使用
split()
即可。just use
split()
.如果您只使用
.split()
而不是.split(' ')
,它不会将空格添加为元素It won't add whitespace as elements if you just use
.split()
, instead of.split(' ')
就像文档所说,不要传递参数。
Like the docs say, don't pass an argument.