May I suggest random.shuffle()
?
def scramble(word):
foo = list(word)
random.shuffle(foo)
return ''.join(foo)
To scramble the order of words:
words = input.split()
random.shuffle(words)
new_sentence = ' '.join(words)
To scramble each word in a sentence, preserving the order:
new_sentence = ' '.join(scramble(word) for word in input.split())
If it's important to preserve the first and last letters as-is:
def scramble(word):
foo = list(word[1:-1])
random.shuffle(foo)
return word[0] + ''.join(foo) + word[-1]