使用字符串定界符分割字符串,并与定界符一起显示数据

时间:2019-12-27 08:23:24

标签: python split

我需要获取字符串列表以及定界符 例如

string= "I am a developer and i need to work on web development"

我正在使用

re.split("and", string)```

I need to get Output as
**Output**
```I am developer 
and i need to work on web development```

2 个答案:

答案 0 :(得分:0)

这是使用str.split函数的简单解决方案:

string = "I am a developer and I need to work on web development"
delimiter = "and"

string_1, string_2 = string.split(delimiter, maxsplit=1)
string_2 = f"{delimiter}{string_2}" # or string_2 = delimiter + string_2

print(string_1) # I am a developer 
print(string_2) # and I need to work on web development

这显然是基于分隔符在字符串中这一事实。

答案 1 :(得分:0)

尝试一下:

>>> s = "I am a developer and i need to work on web development"
>>> s1 = s.replace('and', '\nand')
>>> s1
'I am a developer \nand i need to work on web development'
>>> print(s1)
I am a developer 
and i need to work on web development
>>>