python - Dynamic padding with period using f-string - Stack Overflow
python - Format string dynamically - Stack Overflow
Using an f-string with multiple parameters (decimal places plus string padding)
f-string and string right padding is driving me crazy
You can do this using the str.format() method.
>>> width = 20
>>> print("{:>{width}} : {:>{width}}".format("Python", "Very Good", width=width))
Python : Very Good
Starting from Python 3.6 you can use f-string to do this:
In [579]: lang = 'Python'
In [580]: adj = 'Very Good'
In [581]: width = 20
In [582]: f'{lang:>{width}}: {adj:>{width}}'
Out[582]: ' Python: Very Good'
You can fetch the padding value from the argument list:
print '%*s : %*s' % (20, "Python", 20, "Very Good")
You can even insert the padding values dynamically:
width = 20
args = ("Python", "Very Good")
padded_args = zip([width] * len(args), args)
# Flatten the padded argument list.
print "%*s : %*s" % tuple([item for list in padded_args for item in list])
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
I know this is dumb, but something is strange. Everything works from the python3 interpreter but in my code it doesn't work. All I want is a string to be padded to the right.
This doesn't work
ename = f'"{endpoint}"'
print(f'{header} {ename:<30}!') I always get a single space before the ! I expect to get this (which I get in the interactive mode)
HEADER ENAME....................!
(note: I put dots there to make the spaces clear, they should be spaces!)
But I get
HEADER ENAME.!
If I change it to this, it works
ename = f'"{endpoint}"'
print(f'{header} {ename:!<30}!')But the padding is ! marks. (which was a test). If I change the ! to a space, that doesn't work.
Clearly I'm being dumb and missing something important. What an I missing? Thanks.
UPDATE: Appears to be caused by some post processing after python runs. Not sure what it is, but I don't believe it's python at the moment. Strange indeed!
UPDATE2: Well, I'm dumb. Problem was that this ultimately went through an HTML renderer and that would remove multiple spaces (exactly as it should). So 100% not a python issue. Thanks to all.
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!'
Use the str.format() method of string formatting instead:
'{number:0{width}d}'.format(width=2, number=4)
Demo:
>>> '{number:0{width}d}'.format(width=2, number=4)
'04'
>>> '{number:0{width}d}'.format(width=8, number=4)
'00000004'
The str.format() formatting specification allows for multiple passes, where replacement fields can fill in parameters for formatting specifications.
In the above example, the hard-coded padding width specification would look like:
'{:02d}'.format(4)
or
'{number:02d}'.format(number=4)
with a named parameter. I've simply replaced the 2 width specifier with another replacement field.
To get the same effect with old-style % string formatting you'd need to use the * width character:
'%0*d' % (width, number)
but this can only be used with a tuple of values (dictionary formatting is not supported) and only applies to the field width; other parameters in the formatting do not support dynamic parameters.
Yes, it's possible
>>> print "%0*d" % (5, 4)
00004
>>> print "%0*d" % (10, 4)
0000000004