Help mix letters in words text nothing happens in this code
from nltk.tokenize import word_tokenize import random a= "some text" a1= word_tokenize (a) for i in a1: random.shuffle (i.split ()) print ('' .join (a1))
Answer # 1
import random text= "some text" words= text.split () for i, word in enumerate (map (list, words)): random.shuffle (word) words [i]= '' .join (word) print (* words) # -> eosm txet
Strings are immutable in Python, so to shuffle letters in words of text, each word is converted to a list of characters, the list is shuffled, then the concatenated characters are assigned back to the word list.
In general, words can be broken into letters not only character by character (into separate Unicode code points). Cm. How do I split a string into separate characters?
Answer # 2
I can suggest this option:
import random slovo= 'Hello' slovo_list= list (slovo) abrakadabra= random.sample (slovo_list, len (slovo_list)) print (abrakadabra)