mix up a string

Discussion in 'Python' started by rbt, Jun 5, 2005.

  1. rbt

    rbt Guest

    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
    rbt, Jun 5, 2005
    #1
    1. Advertising

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

    Try:

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

    Skip
    Skip Montanaro, Jun 5, 2005
    #2
    1. Advertising

  3. rbt wrote:
    > 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
    Reinhold Birkenfeld, Jun 5, 2005
    #3
  4. rbt

    rbt Guest

    Reinhold Birkenfeld wrote:
    > rbt wrote:
    >
    >>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


    Thanks guys, this works great. I forgot that shuffle needs a sequence...
    duh ;)
    rbt, Jun 5, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. AFN
    Replies:
    5
    Views:
    401
    dilipdotnet at apdiya.com
    Feb 11, 2004
  2. =?Utf-8?B?UGF1cmF2aQ==?=

    Problem while reading excel file with mix datatypes

    =?Utf-8?B?UGF1cmF2aQ==?=, Mar 2, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    571
    Alvin Bruney [MVP]
    Mar 4, 2004
  3. =?Utf-8?B?Sg==?=

    ASP.NET /Classic ASP Security Mix

    =?Utf-8?B?Sg==?=, Jul 13, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    620
    Raterus
    Jul 13, 2004
  4. Urs Eichmann

    Mix Coldfusion and ASP.NET pages

    Urs Eichmann, Apr 1, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    914
    Steve C. Orr [MVP, MCSD]
    Apr 2, 2005
  5. =?Utf-8?B?QnJpYW4gSG8=?=

    Mix version of .NET Framework Configuration in IIS

    =?Utf-8?B?QnJpYW4gSG8=?=, Jan 5, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    5,772
    Christopher Reed
    Jan 6, 2006
Loading...

Share This Page