mix up a string

R

rbt

What's the best way to take a string such as 'dog' and mix it up? You
know, like the word jumble in the papers? ODG. I thought something like
mix = random.shuffle('dog') would do it, but it won't. Any tips?

Thanks,
rbt
 
S

Skip Montanaro

rbt> mix = random.shuffle('dog')

Try:

lst = list('dog')
random.shuffle(lst)
print "".join(lst)

Skip
 
R

Reinhold Birkenfeld

rbt said:
What's the best way to take a string such as 'dog' and mix it up? You
know, like the word jumble in the papers? ODG. I thought something like
mix = random.shuffle('dog') would do it, but it won't. Any tips?

py> def shuffled(s):
.... l = list(s)
.... random.shuffle(l)
.... return ''.join(l)


Reinhold
 
R

rbt

Reinhold said:
py> def shuffled(s):
... l = list(s)
... random.shuffle(l)
... return ''.join(l)


Reinhold

Thanks guys, this works great. I forgot that shuffle needs a sequence...
duh ;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top