python - "/1/2/3/".split( "/")

标签 python string

太热了,我可能是智障了。

>>> "/1/2/3/".split("/")
['', '1', '2', '3','']

开头和结尾的空元素是怎么回事?

编辑:谢谢大家,我把这归结为热引起的脑衰竭。虽然文档不是最清楚的,来自 http://docs.python.org/library/stdtypes.html

"Return a list of the words in the string, using sep as the delimiter string"

在第一个“/”之前或最后一个“/”之后有一个词吗?

最佳答案

比较:

"1/2/3".split("/")

空元素仍然是元素。

您可以使用 strip('/') 去除字符串开头/结尾的分隔符。

关于python - "/1/2/3/".split( "/"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3142428/

相关文章:

python - 如何计算数据框中两列之间的相关系数?

android - 复数不能按预期在 android 中工作

python - 为什么我的代码在从 dict 中提取值时不起作用

python - 在 python 中与 slurm 作业交互

python - 如何在导致python脚本错误的大数据上查找特定行?

python - 如何打印使用类实现的树数据结构?

C++ 不确定如何让我的程序输出空格的位置

java - 字符串[]转字符串

python - 计算字符串中的字符串

C++如何检查字母是否是alpha(不是拉丁字母)