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 OverflowVideos
Allow f-string to dynamically pad spaces
Using an f-string with multiple parameters (decimal places plus string padding)
f-string and string right padding is driving me crazy
What are some cool f-string tricks that you've learned?
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 '
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!'
Looking for some assistance here.
I can clearly do this with multiple steps, but I'm wondering the optimal way.
if I have a float 12.34, I want it to print was "12___" (where the underscores just exist to highlight the spaces. Specifically, I want the decimals remove and the value printed padded to the right 5 characters.
The following does NOT work, but it shows what I'm thinking
print(f'{myFloat:.0f:<5}')
Is there an optimal way to achieve this? Thanks