假设我有以下字符串:
"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位置打印所有单词的建议吗?
答案 0 :(得分:3)
像这样,假设words
是字符串:
print words.split()[4:10]
答案 1 :(得分:3)
假设你需要从第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']
答案 2 :(得分:-4)
这是基本的东西,你会这样做:
print "my words are right here haha lol"[5:11]
您最有可能看一下:http://effbot.org/zone/python-list.htm因为python字符串与list类似,并且可以对它们执行许多列表函数,例如索引,这是您的问题所要求的。