改变一个单词的一部分

时间:2012-11-27 20:06:50

标签: python string list

我正在试图弄清楚如何做到这一点:

>>>sentence = "There's something right there."
>>>change_s(sentence)
['there', 'his', 'something', 'right', 'there.']
>>>sentence = "it's Patrick's car."
>>>change_s(sentence)
['it', 'his', 'Patrick', 'his', 'car.']

所以基本上我想把“'s”改为“他的”(即使它在语法上不正确)并将这些单词放在一个列表中。

1 个答案:

答案 0 :(得分:1)

In [6]: sentence = "There's something right there."

In [7]: sentence.replace("'s", " his").split()
Out[7]: ['There', 'his', 'something', 'right', 'there.']