Python: the easiest way to get a string of spaces of length N

What is the easiest way to create a string of spaces of length N in Python?

(besides something similar, which is multi-line and supposedly inefficient for large n:

def spaces(n): s = '' for i in range(n): s += ' ' return s 

)

+4
source share
6 answers

try this, just one line only:

  ' ' * n 
+15
source

It's simple:

 s = ' ' * N 
+3
source

You can do this as a function:

 def spaces(n): return ' ' * n 

Or just use the expression:

 ' ' * n 
+2
source

Above my head:

 spaces = lambda x: ' ' * x 
+1
source

All these scammers are not funny, and their simple answers stink.

The true answer is:

 def give_me_spaces(n): return (lambda : ''.join(' ' for _ in xrange(n)))() 

Also, it is clearly superior because it uses functional programming.

0
source

Had to dig through my python helper functions to find this, but here it is. It served me well.

 spaces = (lambda n: (lambda z: z(lambda f: lambda m:'' if m <= 0 else ' ' + f(m - 1))(n)) (lambda f:(lambda x:f(lambda*a:x(x)(*a)))(lambda x:f(lambda*a:x(x)(*a))))) 
0
source

All Articles