tit*_*h99 1 python string split python-3.x
我试图将我的字符串分成 str = 'I HAVE 6'两部分.输出应该
['I HAVE','6']
是没有关于作为输入给出的单词长度的信息.一种方法是将其拆分为三个并添加前两个.还有其他方法吗?
您可以使用rsplit,这从右边分割,用maxsplit的1:
s = 'I HAVE 6'
s.rsplit(maxsplit=1) # or (' ', 1)
['I HAVE', '6']
Run Code Online (Sandbox Code Playgroud)
另外,不要使用名称str.它掩盖了内置类str,并将导致错误.