You can do this with str.ljust(width[, fillchar]):
Return the string left justified in a string of length width. Padding is done using the specified fillchar (default is a space). The original string is returned if width is less than
len(s).
>>> 'hi'.ljust(10)
'hi '
Answer from Felix Kling on Stack OverflowYou can do this with str.ljust(width[, fillchar]):
Return the string left justified in a string of length width. Padding is done using the specified fillchar (default is a space). The original string is returned if width is less than
len(s).
>>> 'hi'.ljust(10)
'hi '
For a flexible method that works even when formatting complicated string, you probably should use the string-formatting mini-language,
using either f-strings
>>> f'{"Hi": <16} StackOverflow!' # Python >= 3.6
'Hi StackOverflow!'
or the str.format() method
>>> '{0: <16} StackOverflow!'.format('Hi') # Python >=2.6
'Hi StackOverflow!'
Python 3, best way to pad/remove spaces in between strings?
How do I store user input (with spaces) in a string?
f-string and string right padding is driving me crazy
how to remove leading space in formatting?
Lets say I have several strings like:
name = "John" occupation = "Plumber"
I want it to print out in such a way that name starts at the very left and occupation starts 15 characters from the left. So if name was "Christian" then there would be less space in between Christian and Plumber than John and Plumber because Christian is longer.
Example output:
John Plumber Christian Plumber