1

I am trying to make a script that will generate a random string of text when i run it.

I have got far but im having a problem with formatting.

Here is the code im using

import random    
alphabet = 'abcdefghijklmnopqrstuvwxyz'

min = 5
max = 15

name = random.sample(alphabet,random.randint(min,max))

print name

And when ever i end up with this

['i', 'c', 'x', 'n', 'y', 'b', 'g', 'r', 'h', 'p', 'w', 'o']

I am trying to format so it is one line of string so for example

['i', 'c', 'x', 'n', 'y', 'b', 'g', 'r', 'h', 'p', 'w', 'o'] = icxnybgrhpwo
1
  • 2
    Note that the sample function always chooses a set of unique letters, so you'll run into a problem if max is greater than 26 (ValueError: sample larger than population). Commented Sep 23, 2011 at 5:22

3 Answers 3

12

join() it:

>>> name = ['i', 'c', 'x', 'n', 'y', 'b', 'g', 'r', 'h', 'p', 'w', 'o']
>>> ''.join(name)
'icxnybgrhpwo'
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome it worked i put the variable in there i came up with ''.join(name)
1
import string
import random

def create_random(length=8):
    """ Create a random string of {length} length """
    chars = string.letters + string.digits
    return ''.join(random.Random().sample(chars, length))

Comments

0

An easy way to print an alphabet :

>>> import string
>>> string.ascii_lowercase
'abcdefghijklmnopqrstuvwxyz'

(source)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.