0

假设我有以下字符串:

"Python is a programming language that lets you work more quickly and integrate your systems more effectively. You can learn to use Python and see almost immediate gains in productivity and lower maintenance costs."

起始字位置:5
结束字位置:10

从 5 到 10 的位置打印所有单词有什么建议吗?

4

3 回答 3

3

假设您需要从位置 5 到 10 的整个单词(而不是字符)(包括位置 5 到 10),您可以执行以下操作:

print sentence.split(" ")[4:10]

例如:

>>> print "Python is a programming language that lets you work more quickly \
       and integrate your systems more effectively. You can learn to use \
       Python and see almost immediate gains in productivity and lower \
       maintenance costs.".split(" ")[4:10]

['language', 'that', 'lets', 'you', 'work', 'more']
于 2012-12-17T13:48:57.720 回答
3

像这样,假设words是字符串:

print words.split()[4:10]
于 2012-12-17T13:45:32.727 回答
-4

这是基本的东西,你会这样做:

print "my words are right here haha lol"[5:11]

您很可能应该看一下:http ://effbot.org/zone/python-list.htm ,因为 python 字符串类似于列表,并且可以对它们执行许多列表功能,例如索引,这就是您的问题询问。

于 2012-12-17T13:46:03.293 回答