Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python: easiest way to get a string of spaces of length N

Tags:

python

string

What's the easiest way to generate a string of spaces of length N in Python?

(besides something like this, which is multiline and presumably inefficient for large n:

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

)

like image 988
Jason S Avatar asked Sep 13 '11 21:09

Jason S


2 Answers

You could do it as a function:

def spaces(n):
    return ' ' * n

Or just use the expression:

' ' * n
like image 182
TomZ Avatar answered Nov 12 '22 22:11

TomZ


try this, simple, only one line:

    ' ' * n
like image 44
multipleinterfaces Avatar answered Nov 12 '22 21:11

multipleinterfaces