0

I am making a program which includes designed pattern sometimes when user give a name of 4 characters or 8 characters name the design pattern get destroyed as i have design it by adding spaces etc,i want to pad spaces after whatever length of name is,it add spaces until it reaches the column i defined,i know how to add spaces with ljust() but it only add defined spaces,i want to add spaces till it reaches the defined column

  • 1
    Welcome to StackOverflow! Your question seems a little broad. Take a look at the [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) page as a guide on how improve future questions. On the topic of the question itself, it's hard to tell, but you probably should not be padding the data manually, but rather use something like the [string formatter](https://www.w3resource.com/python/python-format.php). Some good options are detailed [here](https://stackoverflow.com/questions/5676646/how-can-i-fill-out-a-python-string-with-spaces), too – hyperTrashPanda Jul 04 '19 at 08:22

2 Answers2

0

String formatting should be enough to achieve this:

>>> print("'%-12s'" % 'foo')
'foo         '
>>> print"'%12s'" % 'foo')
'         foo'
gogaz
  • 2,323
  • 2
  • 23
  • 31
0

If I understand well your request, you may try this:

>>> print('{0: <20}end.'.format('short'))
short               end.
>>> print('{0: <20}end.'.format('a little bit longer'))
a little bit longer end.

See also the documentation on Format Specification Mini-Language.