【发布时间】:2020-08-30 23:56:51
【问题描述】:
如何用python print() 最后两个单词组成带空格的句子?像“Hello world,有 200 件”,我需要打印:“200 件”。非常感谢。
sentence = "Hello world, there is 200 pcs"
what_I_need = sentence.split()
what_I_need[-3]
# That prints "is"
print(what_I_need)
# But I need to print "is 200 pcs"
【问题讨论】:
-
what_I_need[-3:]? -
这没有意义:
what_I_need[-3]它的计算结果是从右边数第三个单词,但是你丢弃了结果,所以它没有效果。 -
这能回答你的问题吗? Understanding slice notation