I've tried:
text="Sample sentence."
random.shuffle(text)
print(''.join(text))
But this shuffles everything, possible output:
nmactnpleSSe ee
I want something like this:
Smplea ntesence.
I've tried:
text="Sample sentence."
random.shuffle(text)
print(''.join(text))
But this shuffles everything, possible output:
nmactnpleSSe ee
I want something like this:
Smplea ntesence.
Just extract and shuffle the bit you want, and then reassemble it afterwards.
import random
text = "Sample sentence."
text1 = list(text[1:-1])
random.shuffle(text1)
text2 = text[0] + ''.join(text1) + text[-1]
print(text2)
Note: this answers the question as stated regarding shuffling all except the first and last character. The example shown in the question appears to be a special case, where each word gets shuffled separately. This is a possible outcome, but not a guaranteed one.
See below
import random
text = 'Sample sentence.'
lst = [char for char in text[1:-1]]
random.shuffle(lst)
lst.append(text[-1])
lst.insert(0,text[0])
print(''.join(lst))