tags:

views:

253

answers:

8

How do you create a random string in Python?

I needed it to be number then character repeat till you're done this is what I created

def random_id(length):
    number = '0123456789'
    alpha = 'abcdefghijklmnopqrstuvwxyz'
    id = ''
    for i in range(0,length,2):
        id += random.choice(number)
        id += random.choice(alpha)
    return id
+9  A: 

like so:

# Created by throwing my keyboard against wall.  Guaranteed to be random
random_string = "t\b2w57,owe1v5h"
Triptych
`var`!!!?!??!??!
Chris Lutz
lmao I've been writing Javascript all day. Editing.
Triptych
Very nice. I'd upvote, but don't want to obscure the more-likely-to-be-helpful answers...
Blair Conrad
Okay, there's an accepted answer with more votes. I'm going for it.
Blair Conrad
haha this is at 7 upvotes and 2 down. Better than I expected. Did SO grow a sense of humor when I wasn't looking?
Triptych
:) I think seriousness among programmers is taken much too serious. Seriously!
Felix Kling
+2  A: 

You can build random ascii characters like:

import random
print chr(random.randint(0,255))

And then build up a longer string like:

len = 50
print ''.join( [chr(random.randint(0,255)) for i in xrange(0,len)] )
Ross Rogers
Why use string formatting? `''.join(map(chr, random.randint(0,256) for _ in xrange(len)))`
Chris Lutz
yeah, that would be faster. editting...
Ross Rogers
+13  A: 

Generating strings from (for example) lowercase characters:

import random, string

def randomword(length):
   return ''.join(random.choice(string.lowercase) for i in range(length))

Results:

>>> randomword(10)
'vxnxikmhdc'
>>> randomword(10)
'ytqhdohksy'
sth
Best answer so far. I'd use `randomword(length, source_alpha=string.lowercase)` and `xrange(length)`, though.
Hank Gay
Note that although this is a very good answer, the OP has modified the question to invalidate it. And to provide his/her own answer.
Blair Conrad
+1  A: 

You haven't really said much about what sort of random string you need. But in any case, you should look into the random module.

A very simple solution is pasted below.

import random

def randstring(length=10):
    valid_letters='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    return ''.join((random.choice(valid_letters) for i in xrange(length)))

print randstring()
print randstring(20)
MAK
fyi you can omit the outermost set of parens in your return statement.
recursive
+1  A: 

Here is a recipe to create a random string (password) which is readable.

Noufal Ibrahim
+3  A: 

Since this question is fairly, uh, random, this may work for you:

>>> import uuid
>>> print uuid.uuid4()
58fe9784-f60a-42bc-aa94-eb8f1a7e5c17
Brandon Corfman
In many cases, random isn't really required. Rather, all you really need is unique.
Chase Seibert
+1  A: 
>>> import random
>>> import string
>>> s=string.lowercase+string.digits
>>> ''.join(random.sample(s,10))
'jw72qidagk
ghostdog74
+1  A: 

there is another question on this at http://stackoverflow.com/questions/785058/random-strings-in-python-2-6-is-this-ok

jbwoot