从字符串中删除前导空格
你好,我有 python 中的字符串列表
[" 0", " 1", " 2"]
,我想删除其中一个字符串开头的空格。
我该怎么做?
Hi I have list of strings in python
[" 0", " 1", " 2"]
and I want to remove the whitespace from the start of one of them.
How would I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您想删除所有空白,您可以使用
如果您确实只想删除其中一个,请使用
其中
i
是您要删除的索引。If you want to remove the whitespace from all of them, you could use
If you really only want to strip one of them, use
where
i
is the index of the one you want to strip.要删除空格,您可以使用 strip 方法,然后使用位置运算符访问并分配列表中的该位置,对于位置 0 中的元素,您应该执行以下操作:
To remove the spaces you can use the strip method, then use the positional operator to access and assign that position on the list, for the element in position 0 you should do:
嘘...
boooooh...
如果你想从头开始删除,你可以使用
lstrip()
。否则,您可以使用strip()
删除尾随和前导。you can use
lstrip()
if you want to remove from start. Otherwise, you can usestrip()
to remove trailing and leading.如果您希望结果是整数列表,那么
如果您希望结果是字符串列表,请尝试此操作
If you want the the result to be a list of intergers then try this
else if you want it to be a list of string