How to move the n word to the end of a string in Python

2 Answers

0 votes
s = "Would you like to know more? (Explore and learn)"

n = 2   # zero‑based index → 2 = "third"

parts = s.split()
word = parts.pop(n)
parts.append(word)

result = " ".join(parts)
print(result)



'''
run:

Would you to know more? (Explore and learn) like

'''

 



answered Feb 3 by avibootz
edited Feb 3 by avibootz
0 votes
def move_nth_word_to_end_of_string(s, n):
    parts = s.split()
    word = parts.pop(n)
    parts.append(word)
 
    return " ".join(parts)
 
     
s = "Would you like to know more? (Explore and learn)"
n = 2
  
result = move_nth_word_to_end_of_string(s, n)
print(result)
 
  
  
'''
run:
  
Would you to know more? (Explore and learn) like
  
'''

 



answered Feb 3 by avibootz
edited Feb 6 by avibootz
...