Python 3, 94 93 91 characters
Using a different technique. Might also work in Python 2.
from random import*
s=lambda x:x[0]+''.join(sample(x[1:-1],len(x)-2))+x[-1]if x[0:-1]else x
The ... if x[0:-1] else x
gives x
if its length is 1 (otherwise it would be duplicated). The function thereby works for strings of length 0 and 1.
The sample()
is from http://stackoverflow.com/questions/2668312/shuffle-string-in-python/2668366#2668366https://stackoverflow.com/questions/2668312/shuffle-string-in-python/2668366#2668366.
Since it's one expression, we can use a lambda
(eliminating return
, def
, and a pair of parentheses).
Edit: from random import*
to save 1 character, after the other Python submission.