0

有没有办法根据标点符号来打破字符串

#!/usr/bin/python

#Asking user to Enter a line in specified format
myString=raw_input('Enter your String:\nFor Example:I am doctor break I stays in CA break   you can contact me on +000000\n')
# 'break' is punctuation word 
<my code which breaks the user input based on break word and returns output in different lists>

期望输出像

String1:I am doctor

String2:I stays in CA

String2:you can contact me on +000000

4

2 回答 2

1

您也可以在字符串上使用该split方法,它会根据拆分分隔符返回所有标记的列表。

>>> a="test break testagain break again!"
>>> a.split(" break ")
['test ', ' testagain ', ' again!']
于 2013-10-11T07:14:26.430 回答
1

基于的regex解决方案,这也将处理尾随和前导空格:

>>> import re
>>> text = "I am doctor break I stays in CA break   you can contact me on +000000\n"
>>> re.split(r'\s+break\s+', text)
['I am doctor', 'I stays in CA', 'you can contact me on +000000\n']
于 2013-10-11T07:29:45.443 回答