If you can use repeated letters, you can use the *
operator:
>>> 'a'*5
'aaaaa'
This might be a little off the question, but for those interested in the randomness of the generated string, my answer would be:
import os
import string
def _pwd_gen(size=16):
chars = string.letters
chars_len = len(chars)
return str().join(chars[int(ord(c) / 256. * chars_len)] for c in os.urandom(size))
if you just want any letters:
'a'*10 # gives 'aaaaaaaaaa'
if you want consecutive letters (up to 26):
''.join(['%c' % x for x in range(97, 97+10)]) # gives 'abcdefghij'
Why "one line"? You can fit anything onto one line.
Assuming you want them to start with 'a', and increment by one character each time (with wrapping > 26), here's a line:
>>> mkstring = lambda(x): "".join(map(chr, (ord('a')+(y%26) for y in range(x))))
>>> mkstring(10)
'abcdefghij'
>>> mkstring(30)
'abcdefghijklmnopqrstuvwxyzabcd'
The first ten lowercase letters are string.lowercase[:10]
(if you have imported the standard library module string
previously, of course;-).
Other ways to "make a string of 10 characters": 'x'*10
(all the ten characters will be lowercase x
s;-), ''.join(chr(ord('a')+i) for i in xrange(10))
(the first ten lowercase letters again), etc, etc;-).
Source: Stackoverflow.com