尝试在数字 7
处拆分字符串,我希望将 7 包含在拆分字符串的第二部分中。
代码:
a = 'cats can jump up to 7 times their tail length'
words = a.split("7")
print(words)
输出:
['cats can jump up to ', ' times their tail length']
字符串被拆分,但第二部分不包括 7。
我想知道如何包含 7。
注意:不是 Python split() without removing the delimiter 的副本因为分隔符必须是第二个字符串的一部分。
最佳答案
一个简单而天真的方法就是找到你想要分割的索引并将其切片:
>>> a = 'cats can jump up to 7 times their tail length'
>>> ind = a.index('7')
>>> a[:ind], a[ind:]
('cats can jump up to ', '7 times their tail length')
关于python - python3.5中使用split函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48950806/