Python: Cut off the last word of a sentence?

后端 未结 7 1563
攒了一身酷
攒了一身酷 2020-12-08 09:17

What\'s the best way to slice the last word from a block of text?

I can think of

  1. Split it to a list (by spaces) and removing the last item, then reco
7条回答
  •  情书的邮戳
    2020-12-08 09:55

    If you want to keep your current method, use ' '.join(words) to concatenate the list.

    You also might want to replace words = words[len[words -1] with words = words[:-1] to make use of list slicing.

提交回复
热议问题