简体   繁体   中英

Add first word to the last of a sentence in python

I have this sentence: 'Johnny Johnny yes papa'. And I want to put the first element of this sentence (as a list) again at the end. This is my code:

sentence = 'Johnny Johnny yes papa'
if len(sentence)>0:
  sentence = sentence.split()
  sentence = sentence.extend(sentence[0])

But it returns me a none object. I tried this:

if len(sentence)>0:
  sentence = sentence.split()
  #sentence = sentence.extend(sentence[0])
  sentence = sentence.append(sentence[0])

And again, it returns none. Please, could you help me with this error? The intended list is: ['Johnny','Johnny','yes','papa','Johnny']

Because sentence.append(...) doesn't return anything. It applies on place. You just need to replace sentence = sentence.append(sentence[0]) with sentence.append(sentence[0]) line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM