192

I need to generate a string with n characters in Python. Is there a one line answer to achieve this with the existing Python library? For instance, I need a string of 10 letters:

string_val = 'abcdefghij'
Jon Seigel
  • 12,035
  • 8
  • 56
  • 92
Thierry Lam
  • 43,486
  • 40
  • 114
  • 144
  • 26
    Leave "in one line of code" to code obfuscation contests. When the solution to a problem is naturally written as one line, it will be; otherwise it shouldn't be. Using it as a goal of its own is a guaranteed path to nasty code. – Glenn Maynard Sep 14 '09 at 22:32
  • 3
    Unless, of course, this is homework. In which case, leave the "in one line of code" but be honest and include the [homework] tag. – S.Lott Sep 15 '09 at 00:12
  • 6
    It's actually not a homework question, I just needed a string of n length in my test scripts. I forgot that in Python, a char can be multiplied by n where n is a positive integer to achieve what I want. – Thierry Lam Sep 15 '09 at 01:08
  • refer this http://stackoverflow.com/a/3391106/3779480 – DigviJay Patil Jun 16 '15 at 11:24

6 Answers6

377

To simply repeat the same letter 10 times:

string_val = "x" * 10  # gives you "xxxxxxxxxx"

And if you want something more complex, like n random lowercase letters, it's still only one line of code (not counting the import statements and defining n):

from random import choice
from string import ascii_lowercase
n = 10

string_val = "".join(choice(ascii_lowercase) for i in range(n))
Community
  • 1
  • 1
Eli Courtwright
  • 176,085
  • 65
  • 206
  • 255
12

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 xs;-), ''.join(chr(ord('a')+i) for i in xrange(10)) (the first ten lowercase letters again), etc, etc;-).

Alex Martelli
  • 811,175
  • 162
  • 1,198
  • 1,373
6

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'
Peter
  • 121,125
  • 53
  • 174
  • 208
4

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'
John Millikin
  • 191,086
  • 39
  • 207
  • 222
4

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))

See these answers and random.py's source for more insight.

foudfou
  • 836
  • 6
  • 10
3

If you can use repeated letters, you can use the * operator:

>>> 'a'*5

'aaaaa'
Fragsworth
  • 31,191
  • 25
  • 79
  • 97