You essentially split the list, change it, and then rotate.
So this works:
>>> st='the price of food is 12 dollars' >>> li=st.split()[::-1] >>> n=3 >>> print ' '.join(l[n:]+l[:n]) food of price the dollars 12 is
Or, more directly:
>>> li='the price of food is 12 dollars'.split()[::-1] >>> print ' '.join(li[3:]+li[:3]) food of price the dollars 12 is
Or if you want it in a function:
def chunk(st,n): li=st.split()[::-1]
Key:
st='the price of food is 12 dollars' # the string li=st.split() # split that li=li[::-1] # reverse it li=li[3:]+li[:3] # rotate it ' '.join(li) # produce the string from 'li'
source share