How do i generate it? The string
+11
A:
''.join(random.choice(string.lowercase) for x in range(X))
Ignacio Vazquez-Abrams
2009-12-24 07:54:30
Nice use of generator expressions. But while we're saving memory, might as well use xrange instead of range.
Zach Bialecki
2009-12-24 08:10:25
CytokineStorm, as of Python 3.x, `range()` behaves the same as `xrange()`.
Evan Fosmark
2009-12-24 09:28:48
+3
A:
If you want no repetitions:
import string, random
''.join(random.sample(string.ascii_lowercase, X))
If you DO want (potential) repetitions:
import string, random
''.join(random.choice(string.ascii_lowercase) for _ in xrange(X)))
That's assuming that by a-z
you mean "ASCII lowercase characters", otherwise your alphabet might be expressed differently in these expression (e.g., string.lowercase
for "locale dependent lowercase letters" that may include accented or otherwise decorated lowercase letters depending on your current locale).
Alex Martelli
2009-12-24 08:00:04
FYI, `string.lowercase` is rarely more than ASCII however you set the locale in my experience. I'm just noting that there is no true variable for "current alphabet".
kaizer.se
2009-12-24 10:39:52