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'
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'
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))
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;-).
.lowercase (ascii_lowercase is in recent Python 2's as well as in Python 3).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'
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))
You can use string formatting to ensure a string has exactly n characters. For example, let's say you have a decimal portion of a number that you want to parse with some specific precision:
>>> n = 3
>>> s = "12"
>>> f"{s:0<{n}.{n}}"
120
>>> s = "1234"
>>> f"{s:0<{n}.{n}}"
123
The padding character is '0' in this case, but you can set it to whatever value you want.
Assuming - the original request is about formatting of result-string, where the essential part of the string is known, has arbitrary length and should be extended with some additional symbols (e.g. spaces) to get same length of n characters. In this case the solution might look like this:
n = 10
items = ['1', 777, 'my str', '1000', '', 0] # example of several items reflecting different string-length
for i in items:
print( ' '*(n-len(str(i))) + str(i) ) # string-extension as a single-line
However if original ("known") strings might have length bigger than n - the result length will be also bigger than n. In such case pre-processing might be needed to get maximal length of original strings and adapt the n correspondingly. Implementing of such pre-processing in a single-line could be quite ugly to understand and maintain:
n = 10
items = ['1', 777, 'my str', '1000', '', 0, '012345678901'] # example of several items reflecting different string-length
for i in items:
print( ' '*(max([len(str(k)) for k in items]+[n])-len(str(i))) + str(i) ) # string-extension as a single-line with considering case when items might have string-length bigger than n
Therefore - as already proposed by other replies and comments - a single-line solution should be only used when it's meaningful, easy to implement and clear for others. By all other cases - either implement it at several lines, or - if explicitly needed as a single line solution (e.g. by automatic re-factoring) - define a separated function for required functionality and call it within single line.